public static byte[] PacketToByte(PacketBase packet) { using (var ms = new MemoryStream()) { using (var bw = new BinaryWriter(ms)) { packet.WriteTo(bw); var packetInfo = PacketLengthManager.GetPacketInformation((PACKET_COMMAND)packet.Command); if (packet.GetType().IsSubclassOf(typeof(PacketVarSize))) { ((PacketVarSize)packet).SetLength(bw); } var send = ms.ToArray(); if (!IsPacketLengthSane(packet, ms)) { if (packetInfo != null) { Logger.ErrorFormat("Client::Send() {0} (0x{1:X4}) wrong length {2} != {3}", ((PACKET_COMMAND)packet.Command), packet.Command, ms.Position, packetInfo); } else { Logger.ErrorFormat("Client::Send() {0} (0x{1:X4}) wrong length {2}", ((PACKET_COMMAND)packet.Command), packet.Command, ms.Position); } throw new ApplicationException(); } return(send); } } }