private bool CompleteRead(IAsyncResult ar) { try { int read = Stream.EndRead(ar); if (read == 0) { // client disconnected Close(); return(false); } int size = BitConverter.ToUInt16(InMessage.Buffer, 0) + 2; while (read < size) { if (Stream.CanRead) { read += Stream.Read(InMessage.Buffer, read, size - read); } } InMessage.Resize(size); InMessage.GetUInt16(); // total length return(true); } catch (Exception e) { // TODO: I must not swallow exceptions. // TODO: is closing the connection really necesary? Console.WriteLine(e.ToString()); Close(); } return(false); }