def connect(self, clean_session=True):
    self.sock = socket.socket()
    addr = socket.getaddrinfo(self.server, self.port)[0][(- 1)]
    self.sock.connect(addr)
    if self.ssl:
        import ussl
        self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
    premsg = bytearray(b'\x10\x00\x00\x00\x00\x00')
    msg = bytearray(b'\x04MQTT\x04\x02\x00\x00')
    sz = ((10 + 2) + len(self.client_id))
    msg[6] = (clean_session << 1)
    if (self.user is not None):
        sz += (((2 + len(self.user)) + 2) + len(self.pswd))
        msg[6] |= 192
    if self.keepalive:
        assert (self.keepalive < 65536)
        msg[7] |= (self.keepalive >> 8)
        msg[8] |= (self.keepalive & 255)
    if self.lw_topic:
        sz += (((2 + len(self.lw_topic)) + 2) + len(self.lw_msg))
        msg[6] |= ((4 | ((self.lw_qos & 1) << 3)) | ((self.lw_qos & 2) << 3))
        msg[6] |= (self.lw_retain << 5)
    i = 1
    while (sz > 127):
        premsg[i] = ((sz & 127) | 128)
        sz >>= 7
        i += 1
    premsg[i] = sz
    self.sock.write(premsg, (i + 2))
    self.sock.write(msg)
    self._send_str(self.client_id)
    if self.lw_topic:
        self._send_str(self.lw_topic)
        self._send_str(self.lw_msg)
    if (self.user is not None):
        self._send_str(self.user)
        self._send_str(self.pswd)
    resp = self.sock.read(4)
    assert ((resp[0] == 32) and (resp[1] == 2))
    if (resp[3] != 0):
        raise MQTTException(resp[3])
    return (resp[2] & 1)
def connect(self, clean_session=True):
    self.sock = socket.socket()
    addr = socket.getaddrinfo(self.server, self.port)[0][(- 1)]
    self.sock.connect(addr)
    if self.ssl:
        import ussl
        self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
    premsg = bytearray(b'\x10\x00\x00\x00\x00\x00')
    msg = bytearray(b'\x04MQTT\x04\x02\x00\x00')
    sz = ((10 + 2) + len(self.client_id))
    msg[6] = (clean_session << 1)
    if (self.user is not None):
        sz += (((2 + len(self.user)) + 2) + len(self.pswd))
        msg[6] |= 192
    if self.keepalive:
        assert (self.keepalive < 65536)
        msg[7] |= (self.keepalive >> 8)
        msg[8] |= (self.keepalive & 255)
    if self.lw_topic:
        sz += (((2 + len(self.lw_topic)) + 2) + len(self.lw_msg))
        msg[6] |= ((4 | ((self.lw_qos & 1) << 3)) | ((self.lw_qos & 2) << 3))
        msg[6] |= (self.lw_retain << 5)
    i = 1
    while (sz > 127):
        premsg[i] = ((sz & 127) | 128)
        sz >>= 7
        i += 1
    premsg[i] = sz
    self.sock.write(premsg, (i + 2))
    self.sock.write(msg)
    self._send_str(self.client_id)
    if self.lw_topic:
        self._send_str(self.lw_topic)
        self._send_str(self.lw_msg)
    if (self.user is not None):
        self._send_str(self.user)
        self._send_str(self.pswd)
    resp = self.sock.read(4)
    assert ((resp[0] == 32) and (resp[1] == 2))
    if (resp[3] != 0):
        raise MQTTException(resp[3])
    return (resp[2] & 1)