// Messages from the broker come back to us as publish messages
        private int handlePUBLISH(Socket mySocket, byte firstByte)
        {
            int remainingLength = 0;
            int messageID = 0;
            int topicLength = 0;
            int topicIndex = 0;
            int payloadIndex = 0;
            int index = 0;
            byte[] buffer = null;
            byte[] topic = null;
            byte[] payload = null;
            int QoS = 0x00;
            String topicString = null;
            String payloadString = null;

            remainingLength = undoRemainingLength(mySocket);
            buffer = new byte[remainingLength];
            if ((mySocket.Receive(buffer, 0) != remainingLength) || remainingLength < 5)
                return Constants.ERROR;
            topicLength += buffer[index++] * 256;
            topicLength += buffer[index++];
            topic = new byte[topicLength];
            while (topicIndex < topicLength)
            {
                topic[topicIndex++] = buffer[index++];
            }
            QoS = firstByte & 0x06;
            if (QoS > 0)
            {
                messageID += buffer[index++] * 256;
                messageID += buffer[index++];
                _logger.Debug("PUBLISH: Message ID: " + messageID);
            }
            topicString = new String(Encoding.UTF8.GetChars(topic));
            _logger.Debug("PUBLISH: Topic: " + topicString);
            payload = new byte[remainingLength - index];
            while (index < remainingLength)
            {
                payload[payloadIndex++] = buffer[index++];
            }

            _logger.Debug("PUBLISH: Payload Length: " + payload.Length);

            // This doesn't work if the payload isn't UTF8
            payloadString = new String(Encoding.UTF8.GetChars(payload));
            _logger.Debug("PUBLISH: Payload: " + payloadString);

            MqttPublishMessage msg = new MqttPublishMessage(topicString, new MqttPayload(payloadString), false, (QoS)QoS);
            OnPublishArrived(msg);

            return Constants.SUCCESS;
        }
        protected void OnPublishArrived(MqttPublishMessage m)
        {
            bool accepted = false;

            if (PublishArrived != null)
            {
                PublishArrivedArgs e = new PublishArrivedArgs(m.Topic, m.Payload, m.Retained, m.QualityOfService);
                try
                {
                    accepted |= PublishArrived(this, e);
                }
                catch (Exception ex)
                {
                    _logger.Error("Uncaught exception from user delegate: " + ex.ToString());
                }
            }
        }