 
 import Foundation
 
-enum MQTTMessage : UInt8 {
+extension UInt16 {
+    var lowByte: UInt8 {
+        return UInt8(self & 0x00FF)
+    }
+
+    var highByte: UInt8 {
+        return UInt8((self & 0xFF00) >> 8)
+    }
+}
+
+enum MQTTMessage: UInt8 {
     case CONNECT = 0x10
     case CONNACK = 0x20
     case PUBLISH = 0x30

enum MQTTClientState {
    case ConnectionClosed
    case Initializing
    case DecodingHeader
    case DecodingLength
    case DecodingData
    case ConnectionError
}

protocol LightMQTTDelegate {
    func didReceiveMessage(topic: String, message: String)
}

final class LightMQTT: NSObject, NSStreamDelegate {

    let MQTT_BUFFER_SIZE: Int = 4096
    let MQTT_KEEPALIVE: UInt8 = 10

    private var clientState = MQTTClientState.ConnectionClosed


                    if count > 0 {
                        if let message = MQTTMessage(rawValue: readBuffer[0] & 0xf0) {
                            switch message {
                            case .CONNACK:
                                break
                            case .SUBACK:
                                break
                            case .DISCONNECT:
                                clientState = .ConnectionClosed
                            case .PUBLISH:
                                clientState = .DecodingLength
                            default:
                                break
                            }
                        }
