code: def connect(self, clientId=0, host="localhost", port=4001):
          
          self.clientId = clientId
          self.host = host
          self.port = port
          self.ibConn = Connection.create(
              host=self.host,
              port=int(self.port),
              clientId=self.clientId
          )
  
          # Assign server messages handling function.
          self.ibConn.registerAll(self.handleServerEvents)
  
          # connect
          self.log.info("[CONNECTING TO IB]")
          self.ibConn.connect()
  
          # get server time
          self.getServerTime()
  
          # subscribe to position and account changes
          self.subscribeAccount = False
          self.requestAccountUpdates(subscribe=True)
  
          self.subscribePositions = False
          self.requestPositionUpdates(subscribe=True)
  
          # load working orders
          self.requestOpenOrders()
  
          # force refresh of orderId upon connect
          self.handleNextValidId(self.orderId)
  
          self._disconnected_by_user = False
          time.sleep(1)
docs: Establish connection to TWS/IBGW