private void OnRead(IAsyncResult ar) { try { int receivedBytes = Stream.EndRead(ar); TotalBuffer = Concat(TotalBuffer, Buffer, receivedBytes); while (TotalBuffer.Length > 8) { int encryptedLength = BitConverter.ToInt32(TotalBuffer, 0); int decryptedLength = BitConverter.ToInt32(TotalBuffer, 4); if (TotalBuffer.Length >= 8 + encryptedLength) { //string receivedText = Encoding.ASCII.GetString(buffer, 0, receivedBytes); byte[] PartialBuffer = TotalBuffer.Skip(8).Take(encryptedLength).ToArray(); String Decrypted = Crypting.DecryptStringFromBytes(PartialBuffer); string[] packetData = Regex.Split(Decrypted, "\r\n"); HandleData(packetData); TotalBuffer = TotalBuffer.Skip(encryptedLength + 8).Take(TotalBuffer.Length - encryptedLength - 8).ToArray(); } else { break; } } } catch (IOException) { Program.Disconnect(this); return; } Stream.BeginRead(Buffer, 0, Buffer.Length, new AsyncCallback(OnRead), null); }