private void ProcessReading() { while (true) { byte[] buffer = new byte[BufferSize]; int bytesRead; lock (_syncRoot) bytesRead = _stream.Read(buffer, 0, BufferSize); if (bytesRead == 0) { continue; } byte[] packetBytes = new byte[bytesRead]; Array.Copy(buffer, 0, packetBytes, 0, bytesRead); IEnumerable <Packet> packets = _packetHandler.HandlePackets(packetBytes); foreach (Packet packet in packets) { IMessage message = _messageFactory.CreateMessage(packet.Payload, packet.MessageId); if (message != null) { NotifyForMessage(message); } } } }