if (port == 0) { sp = getservbyname(SSH_SERVICE_NAME, "tcp"); if (sp) port = ntohs(sp->s_port); else port = SSH_DEFAULT_PORT; }
ssh_connect(const char *host, struct sockaddr_storage * hostaddr,    u_short port, int family, int connection_attempts,    int needpriv, const char *proxy_command){	int gaierr;	int on = 1;	int sock = -1, attempt;	char ntop[NI_MAXHOST], strport[NI_MAXSERV];	struct addrinfo hints, *ai, *aitop;	struct servent *sp;	debug2("ssh_connect: needpriv %d", needpriv);	/* Get default port if port has not been set. */	if (port == 0) {		sp = getservbyname(SSH_SERVICE_NAME, "tcp");		if (sp)			port = ntohs(sp->s_port);		else			port = SSH_DEFAULT_PORT;	}	/* If a proxy command is given, connect using it. */	if (proxy_command != NULL)		return ssh_proxy_connect(host, port, proxy_command);	/* No proxy command. */	memset(&hints, 0, sizeof(hints));	hints.ai_family = family;	hints.ai_socktype = SOCK_STREAM;	snprintf(strport, sizeof strport, "%u", port);	if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)		fatal("%s: %.100s: %s", __progname, host,		    gai_strerror(gaierr));	/*	 * Try to connect several times.  On some machines, the first time	 * will sometimes fail.  In general socket code appears to behave	 * quite magically on many machines.		 */	for (attempt = 0; ;) {		if (attempt > 0)			debug("Trying again...");		/* Loop through addresses for this host, and try each one in		   sequence until the connection succeeds. */		for (ai = aitop; ai; ai = ai->ai_next) {			if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)				continue;			if (getnameinfo(ai->ai_addr, ai->ai_addrlen,			    ntop, sizeof(ntop), strport, sizeof(strport),			    NI_NUMERICHOST|NI_NUMERICSERV) != 0) {				error("ssh_connect: getnameinfo failed");				continue;			}			debug("Connecting to %.200s [%.100s] port %s.",				host, ntop, strport);			/* Create a socket for connecting. */			sock = ssh_create_socket(needpriv, ai);			if (sock < 0)				/* Any error is already output */				continue;			if (timeout_connect(sock, ai->ai_addr, ai->ai_addrlen,			    options.connection_timeout) >= 0) {				/* Successful connection. */				memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);				break;			} else {				debug("connect to address %s port %s: %s",				    ntop, strport, strerror(errno));				/*				 * Close the failed socket; there appear to				 * be some problems when reusing a socket for				 * which connect() has already returned an				 * error.				 */				close(sock);			}		}		if (ai)			break;	/* Successful connection. */		attempt++;		if (attempt >= connection_attempts)			break;		/* Sleep a moment before retrying. */		sleep(1);	}	freeaddrinfo(aitop);	/* Return failure if we didn't get a successful connection. */	if (attempt >= connection_attempts) {		error("ssh: connect to host %s port %s: %s",		    host, strport, strerror(errno));		return (-1);	}	debug("Connection established.");	/* Set SO_KEEPALIVE if requested. */	if (options.tcp_keep_alive &&	    setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,	    sizeof(on)) < 0)		error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));	/* Set the connection. */	packet_set_connection(sock, sock);	return 0;}