             return nil
         }
 
-        if useTLS {
+        if options.useTLS {
             input.setProperty(StreamSocketSecurityLevel.tlSv1, forKey: .socketSecurityLevelKey)
             output.setProperty(StreamSocketSecurityLevel.tlSv1, forKey: .socketSecurityLevelKey)
         }


        var messageBytes = headerBytes + encode(string: client)

        if let username = username {
            messageBytes += encode(string: username)
        }

        if let password = password {
            messageBytes += encode(string: password)
        }


        return inputStream?.streamStatus == .open && outputStream?.streamStatus == .open
    }

    struct Options {
        var port: Int? = nil
        var pingInterval: UInt16 = 10
        var useTLS = false
        var username: String? = nil
        var password: String? = nil

        var concretePort: Int {
            return port ?? (useTLS ? 8883 : 1883)
        }
    }

    private var options: Options
    private var host: String
    
    private var inputStream: InputStream?
    private var outputStream: OutputStream?

    private var messageId: UInt16 = 0

    private static let BUFFER_SIZE: Int = 4096

    init(host: String, options: Options = Options()) {
        self.host = host
        self.options = options
    }

    func connect() -> Bool {

        if inputStream != nil || outputStream != nil {
            return false
        }

        guard let (input, output) = openStreams(host: host, port: options.concretePort) else {
            return false
        }

        inputStream = input
        outputStream = output

        mqttConnect(keepalive: options.pingInterval)
        delayedPing(outputStream: output, interval: options.pingInterval)

        messageId = 0

