@handler("connect")  # noqa
    def connect(self, host, port, secure=False, **kwargs):
        # XXX: C901: This has a high McCacbe complexity score of 10.
        # TODO: Refactor this!

        self.host = host
        self.port = port
        self.secure = secure

        if self.secure:
            self.certfile = kwargs.get("certfile", None)
            self.keyfile = kwargs.get("keyfile", None)
            self.ca_certs = kwargs.get("ca_certs", None)

        try:
            r = self._sock.connect((host, port))
        except SocketError as e:
            if e.args[0] in (EBADF, EINVAL,):
                self._sock = self._create_socket()
                r = self._sock.connect_ex((host, port))
            else:
                r = e.args[0]

            if r not in (EISCONN, EWOULDBLOCK, EINPROGRESS, EALREADY):
                self.fire(unreachable(host, port, e))
                self.fire(error(e))
                self._close()
                raise StopIteration()

        stop_time = time() + self.connect_timeout
        while stop_time < time():
            try:
                self._sock.getpeername()
                self._connected = True
                break
            except Exception as e:
                yield

        if not self._connected:
            self.fire(unreachable(host, port))
            raise StopIteration()

        def on_done(sock):
            self._poller.addReader(self, sock)
            self.fire(connected(host, port))

        if self.secure:
            def on_error(sock, err):
                self.fire(error(sock, err))
                self._close()

            self._sock = ssl_socket(
                self._sock, self.keyfile, self.certfile, ca_certs=self.ca_certs,
                do_handshake_on_connect=False
            )
            for _ in do_handshake(self._sock, on_done, on_error):
                yield
        else:
            on_done(self._sock)
dataset/ETHPy150Open circuits/circuits/circuits/net/sockets.py/TCPClient.connect/swapped operands of `<`