}
getport(char *rhost, int rport){	struct addrinfo hints, *res, *r;	u_int timo = 1;	int s, lport = IPPORT_RESERVED - 1;	int error;	int refuse, trial;	char pbuf[NI_MAXSERV];	/*	 * Get the host address and port number to connect to.	 */	if (rhost == NULL)		fatal("no remote host to connect to");	memset(&hints, 0, sizeof(hints));	hints.ai_family = PF_UNSPEC;	hints.ai_socktype = SOCK_STREAM;	if (rport)		snprintf(pbuf, sizeof(pbuf), "%d", rport);	else		snprintf(pbuf, sizeof(pbuf), "printer");	siginterrupt(SIGINT, 1);	error = getaddrinfo(rhost, pbuf, &hints, &res);	siginterrupt(SIGINT, 0);	if (error)		fatal("printer/tcp: %s", gai_strerror(error));	/*	 * Try connecting to the server.	 */retry:	s = -1;	refuse = trial = 0;	for (r = res; r; r = r->ai_next) {		trial++;retryport:		PRIV_START;		s = rresvport_af(&lport, r->ai_family);		PRIV_END;		if (s < 0) {			/* fall back to non-privileged port */			if (errno != EACCES ||			    (s = socket(r->ai_family, SOCK_STREAM, 0)) < 0)				return(-1);		}		siginterrupt(SIGINT, 1);		if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {			error = errno;			siginterrupt(SIGINT, 0);			(void)close(s);			s = -1;			errno = error;			if (errno == EADDRINUSE) {				lport--;				goto retryport;			} else if (errno == ECONNREFUSED)				refuse++;			continue;		} else {			siginterrupt(SIGINT, 0);			break;		}	}	if (s < 0 && trial == refuse && timo <= 16) {		sleep(timo);		timo *= 2;		goto retry;	}	if (res)		freeaddrinfo(res);	/* Don't worry if we get an error from setsockopt(). */	trial = 1;	setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &trial, sizeof(trial));	return(s);}