@Override
	void handleSocket(Socket socket) throws IOException {
		InputStream inputStream = socket.getInputStream();
		DataInputStream dataInputStream = new DataInputStream(inputStream);
		while (true) {
			int identifier = 0;
			try {
				int read = dataInputStream.read();
				if (read == -1) {
					break;
				}

				boolean enhancedFormat = read == 1;
				int expiry = 0;
				if (enhancedFormat) {
					identifier = dataInputStream.readInt();
					expiry = dataInputStream.readInt();
				}

				int deviceTokenLength = dataInputStream.readShort();
				byte[] deviceTokenBytes = toArray(inputStream,
						deviceTokenLength);

				int payloadLength = dataInputStream.readShort();
				byte[] payloadBytes = toArray(inputStream, payloadLength);

				ApnsNotification message;
				if (enhancedFormat) {
					message = new EnhancedApnsNotification(identifier, expiry,
							deviceTokenBytes, payloadBytes);
				} else {
					message = new SimpleApnsNotification(deviceTokenBytes,
							payloadBytes);
				}
				apnsServerService.messageReceived(message);
			} catch (IOException ioe) {
				writeResponse(socket, identifier, 8, 1);
				break;
			} catch (Exception e) {
				writeResponse(socket, identifier, 8, 1);
				break;
			}
		}
	}
Represents the Apple APNS server. This allows testing outside of the Apple
servers.
src/test/java/com/notnoop/apns/ApnsGatewayServerSocket.java