code: def connect(self):
          
  
          # Do not use non-blocking stream, they are not reliably
          # non-blocking
          # s.setblocking(False)
          # s.settimeout(1.0)
  
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          connect_host = "irc.twitch.tv"
          connect_port = 6667
          try:
              s.connect((connect_host, connect_port))
          except (Exception, IOError):
              print("Unable to create a socket to %s:%s" % (
                  connect_host,
                  connect_port))
              raise  # unexpected, because it is a blocking socket
  
          # Connected to twitch
          # Sending our details to twitch...
          s.send(('PASS %s\r\n' % self.oauth).encode('utf-8'))
          s.send(('NICK %s\r\n' % self.username).encode('utf-8'))
          if self.verbose:
              print('PASS %s\r\n' % self.oauth)
              print('NICK %s\r\n' % self.username)
  
          received = s.recv(1024).decode()
          if self.verbose:
              print(received)
          if not TwitchChatStream._logged_in_successful(received):
              # ... and they didn't accept our details
              raise IOError("Twitch did not accept the username-oauth "
                            "combination")
          else:
              # ... and they accepted our details
              # Connected to twitch.tv!
              # now make this socket non-blocking on the OS-level
              fcntl.fcntl(s, fcntl.F_SETFL, os.O_NONBLOCK)
              if self.s is not None:
                  self.s.close()  # close the previous socket
              self.s = s          # store the new socket
              self.join_channel(self.username)
  
              # Wait until we have switched channels
              while self.current_channel != self.username:
                  self.twitch_receive_messages()
docs: Connect to Twitch