code: def connect(self, version = 3, clean_session = 1, will = None):
          
          self.clean_session = clean_session
          self.will          = None
          
          if will is not None:
              self.will = NyamukMsg(
                  topic = will['topic'],
                  # unicode text needs to be utf8 encoded to be sent on the wire
                  # str or bytearray are kept as it is
                  payload = utf8encode(will.get('message','')),
                  qos = will.get('qos', 0),
                  retain = will.get('retain', False)
              )
  
          #CONNECT packet
          pkt = MqttPkt()
          pkt.connect_build(self, self.keep_alive, clean_session, version = version)
          
          #create socket
          self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          if self.ssl:
              opts = {
                  'do_handshake_on_connect': True,
                  'ssl_version': ssl.PROTOCOL_TLSv1
              }
              opts.update(self.ssl_opts)
              #print opts, self.port
  
              try:
                  self.sock = ssl.wrap_socket(self.sock, **opts)
              except Exception, e:
                  self.logger.error("failed to initiate SSL connection: {0}".format(e))
                  return NC.ERR_UNKNOWN
  
          nyamuk_net.setkeepalives(self.sock)
          
          self.logger.info("Connecting to server ....%s", self.server)
          err = nyamuk_net.connect(self.sock,(self.server, self.port))
          #print self.sock.cipher()
          
          if err != None:
              self.logger.error(err[1])
              return NC.ERR_UNKNOWN
          
          #set to nonblock
          self.sock.setblocking(0)
          
          return self.packet_queue(pkt)
docs: Connect to server.