         return remainingBytes
     }
 
+    private func encodeUTF8Length(_ string:String) -> [UInt8] {
+        return [UInt16(string.utf8.count).highByte, UInt16(string.utf8.count).lowByte]
+
+    }
+
 }

         * |----------------------------------------------------------------------------------
         */

        let connectBytes: [UInt8] = [
            0x10,                               // FIXED BYTE 1   1 = CONNECT, 0 = DUP QoS RETAIN, not used in CONNECT
            UInt8(client.utf8.count + 12),      // FIXED BYTE 2   remaining length, client id length + 12
            0x00,                               // VARIA BYTE 1   length MSB
            0x04,                               // VARIA BYTE 2   length LSB is 4
            0x4d,                               // VARIA BYTE 3   M

            0x04,                               // VARIA BYTE 7   Version = 4
            0x02,                               // VARIA BYTE 8   Username Password RETAIN QoS Will Clean flags
            keepalive.highByte,                 // VARIA BYTE 9   Keep Alive MSB
            keepalive.lowByte                   // VARIA BYTE 10  Keep Alive LSB
        ]

        var messageBytes = headerBytes + encodeUTF8Length(client) + [UInt8](client.utf8)
        if let username = self.username {
            messageBytes += (encodeUTF8Length(username) + [UInt8](username.utf8))
        }
        if let password = self.password {
            messageBytes += (encodeUTF8Length(password) + [UInt8](password.utf8))
        }
        outputStream?.write(messageBytes, maxLength: messageBytes.count)
    }

