private void ProcessBuffer(Client Client) { while (m_CurrentlyReceived >= PacketSize) { byte[] PacketBuf = new byte[PacketSize]; Array.Copy(Client.Buffer, PacketBuf, PacketSize); if (PacketType != 0) { lock (Client.ReceivedPackets) Client.ReceivedPackets.Enqueue(new AriesPacket(PacketBuf, true)); ReceivedData.InvokeSafely(this, Client); m_CurrentlyReceived -= (int)PacketSize; } else { VoltronHeader Header = ReadVoltronHeader(PacketBuf, 12); if (Header.PacketSize < (PacketBuf.Length - 12)) { ProcessVoltronPackets(Client, PacketBuf); } else { lock (Client.ReceivedPackets) Client.ReceivedPackets.Enqueue(new VoltronPacket(PacketBuf, true)); ReceivedData.InvokeSafely(this, Client); } m_CurrentlyReceived -= (int)PacketSize; } if (m_CurrentlyReceived > 0) { byte[] Remainder = new byte[m_CurrentlyReceived]; Array.ConstrainedCopy(Client.Buffer, (Client.Buffer.Length - m_CurrentlyReceived) + 1, Remainder, 0, m_CurrentlyReceived); //Recreate the packet buffer and copy the remainder back into it. Client.CreateBuffer(BUFFER_SIZE); Array.Copy(Remainder, Client.Buffer, m_CurrentlyReceived); Remainder = null; } else { Client.CreateBuffer(BUFFER_SIZE); } } }