public byte[] toByteArray() throws PacketEncodingException {
		ByteBuffer buffer = ByteBuffer.allocate(HEADER_SIZE + (message == null ? 0 : message.length()));
		buffer.putInt(protocolId);
		buffer.put(Packet.encodeConnectionId(connectionId));
		buffer.putShort(Packet.encodeSequenceNumber(sequenceNumber));
		buffer.putShort(Packet.encodeSequenceNumber(duplicateSequenceNumber));
		buffer.putShort(Packet.encodeSequenceNumber(lastReceivedSequenceNumber));
		buffer.putInt(receivedPacketHistory);
		buffer.put((isImmediateResponse ? Byte.MIN_VALUE : 0));
		buffer.put(Packet.encodeMessageType(messageType));
		if(message != null)
			buffer.put(message.getBytes());
		return buffer.array();
	}