public override async void Process(object sender, SocketAsyncEventArgs e) { try { var socket = e.UserToken as Socket; var receivedBytes = e.BytesTransferred; if (receivedBytes > 0) { while (receivedBytes > 0) { var packetData = new byte[receivedBytes]; Buffer.BlockCopy(dataBuffer, 0, packetData, 0, receivedBytes); var pkt = new Packet(packetData, (ushort)ClientMessage.Composite); receivedBytes -= (int)pkt.Header.Size; if (pkt.Header.Message != (ushort)GlobalClientMessage.State1 && pkt.Header.Message != (ushort)GlobalClientMessage.State2) { Crypt.Decrypt(pkt.Data, pkt.Data.Length); pkt.ReadMessage(); // Remove the 'Composite' header. receivedBytes -= 6; Buffer.BlockCopy(dataBuffer, (int)pkt.Header.Size + 6, dataBuffer, 0, receivedBytes); PacketLog.Write<ClientMessage>(pkt.Header.Message, pkt.Data, pkt.Data.Length, client.RemoteEndPoint as IPEndPoint); } else Buffer.BlockCopy(dataBuffer, (int)pkt.Header.Size, dataBuffer, 0, receivedBytes); if (receivedBytes > 0) packetQueue.Enqueue(pkt); await ProcessPacket(pkt); } client.ReceiveAsync(e); } else Dispose(); } catch { Dispose(); } }