private byte[] Read() { var header = new MqttFixedHeader(); var headerByte = new byte[1]; int receivedSize; INetworkStream stream = GetStream(); // Read the fixed header do { receivedSize = stream.Receive(headerByte, 0, 1); } while (receivedSize > 0 && header.AppendByte(headerByte[0])); if (!header.IsComplete) { _logger.LogMessage("Socket", LogLevel.Error, "Header data invalid for incoming message."); throw new IOException("Unable to receive the MQTT fixed header."); } _logger.LogMessage("Socket", LogLevel.Verbose, "Begin reading payload for incoming message type: " + header.MessageType); // Create a buffer and read the remaining message var completeBuffer = header.CreateMessageBuffer(); receivedSize = 0; while (receivedSize < header.RemainingLength) { receivedSize += stream.Receive(completeBuffer, header.HeaderSize + receivedSize, header.RemainingLength - receivedSize); } return(completeBuffer); }