def connect(self, host, port, timeout=None): """ Try to establish a connection with host and port. If a timeout is defined, after this timeout has been reached the TCPSocket will stop trying to connect and the socket_connection_timeout TCPSocketDelegate method will be called. If the socket establishes the connection before the timeout socket_did_connect TCPSocketDelegate method will be called. """ #if not self.delegate: # raise SocketException("Attempting to accept without a delegate. Set a delegate first.") if self.socket != None: raise SocketException( "Attempting to accept while connected or accepting connections. Disconnect first." ) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setblocking(0) self.connecting_address = (host, port) self.runloop.register_socket(self) try: self.socket.connect((host, port)) except socket.error as e: if e.errno in (errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK): if timeout: self.connect_timeout = Timer(timeout, self.connection_timeout, False, (host, port), runloop=self.runloop) return return self.close(e) self.did_connect()