int			rc, i, s, err, connected, use_hp;
int			rc = -1; int			s = 0; int			i, err, connected, use_hp;
nsldapi_try_each_host( LDAP *ld, const char *hostlist,	int defport, int secure, NSLDAPI_SOCKET_FN *socketfn,	NSLDAPI_IOCTL_FN *ioctlfn, NSLDAPI_CONNECT_WITH_TO_FN *connectwithtofn,	NSLDAPI_CONNECT_FN *connectfn, NSLDAPI_CLOSE_FN *closefn ){#ifdef NSLDAPI_AVOID_OS_SOCKETS	return -1;#else /* NSLDAPI_AVOID_OS_SOCKETS */	int			rc, i, s, err, connected, use_hp;	int			parse_err, port;	struct sockaddr_in	sin;	nsldapi_in_addr_t	address;	char			**addrlist, *ldhpbuf, *ldhpbuf_allocd;	char			*host;	LDAPHostEnt		ldhent, *ldhp;	struct hostent		*hp;	struct ldap_x_hostlist_status	*status;#ifdef GETHOSTBYNAME_BUF_T	GETHOSTBYNAME_BUF_T	hbuf;	struct hostent		hent;#endif /* GETHOSTBYNAME_BUF_T */	connected = 0;	parse_err = ldap_x_hostlist_first( hostlist, defport, &host, &port,            &status );	while ( !connected && LDAP_SUCCESS == parse_err && host != NULL ) {		ldhpbuf_allocd = NULL;		ldhp = NULL;		hp = NULL;		s = 0;		use_hp = 0;		addrlist = NULL;		if (( address = inet_addr( host )) == -1 ) {			if ( ld->ld_dns_gethostbyname_fn == NULL ) {#ifdef GETHOSTBYNAME_R_RETURNS_INT				(void)GETHOSTBYNAME( host, &hent, hbuf,				    sizeof(hbuf), &hp, &err );#else				hp = GETHOSTBYNAME( host, &hent, hbuf,				    sizeof(hbuf), &err );#endif				if ( hp != NULL ) {					addrlist = hp->h_addr_list;				}			} else {				/*				 * DNS callback installed... use it.				 */#ifdef GETHOSTBYNAME_buf_t				/* avoid allocation by using hbuf if large enough */				if ( sizeof( hbuf ) < ld->ld_dns_bufsize ) {					ldhpbuf = ldhpbuf_allocd					    = NSLDAPI_MALLOC( ld->ld_dns_bufsize );				} else {					ldhpbuf = (char *)hbuf;				}#else /* GETHOSTBYNAME_buf_t */				ldhpbuf = ldhpbuf_allocd = NSLDAPI_MALLOC(				    ld->ld_dns_bufsize );#endif /* else GETHOSTBYNAME_buf_t */				if ( ldhpbuf == NULL ) {					LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY,					    NULL, NULL );					ldap_memfree( host );					ldap_x_hostlist_statusfree( status );					return( -1 );				}				if (( ldhp = ld->ld_dns_gethostbyname_fn( host,				    &ldhent, ldhpbuf, ld->ld_dns_bufsize, &err,				    ld->ld_dns_extradata )) != NULL ) {					addrlist = ldhp->ldaphe_addr_list;				}			}			if ( addrlist == NULL ) {				LDAP_SET_LDERRNO( ld, LDAP_CONNECT_ERROR, NULL, NULL );				LDAP_SET_ERRNO( ld, EHOSTUNREACH );  /* close enough */				if ( ldhpbuf_allocd != NULL ) {					NSLDAPI_FREE( ldhpbuf_allocd );				}				ldap_memfree( host );				ldap_x_hostlist_statusfree( status );				return( -1 );			}			use_hp = 1;		}		rc = -1;		for ( i = 0; !use_hp || ( addrlist[ i ] != 0 ); i++ ) {			if ( -1 == ( s = (*socketfn)( ld, secure, AF_INET,					SOCK_STREAM, 0 ))) {				if ( ldhpbuf_allocd != NULL ) {					NSLDAPI_FREE( ldhpbuf_allocd );				}				ldap_memfree( host );				ldap_x_hostlist_statusfree( status );				return( -1 );			}			if ( ld->ld_options & LDAP_BITOPT_ASYNC ) {				int	iostatus = 1;				err = (*ioctlfn)( s, FIONBIO, &iostatus );				if ( err == -1 ) {					LDAPDebug( LDAP_DEBUG_ANY,					    "FIONBIO ioctl failed on %d\n",					    s, 0, 0 );				}			}			(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));			sin.sin_family = AF_INET;			sin.sin_port = htons( (unsigned short)port );			SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,			    ( use_hp ? (char *) addrlist[ i ] :			    (char *) &address ), sizeof( sin.sin_addr.s_addr) );			{#ifdef NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED/* * Block all of the signals that might interrupt connect() since * there is an OS bug that causes connect() to fail if it is restarted. * Look in ../../include/portable.h for the definition of * NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED. */				sigset_t	ints_off, oldset;				sigemptyset( &ints_off );				sigaddset( &ints_off, SIGALRM );				sigaddset( &ints_off, SIGIO );				sigaddset( &ints_off, SIGCLD );				NSLDAPI_MT_SAFE_SIGPROCMASK( SIG_BLOCK, &ints_off, &oldset );#endif /* NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED */				if ( NULL != connectwithtofn  ) {						err = (*connectwithtofn)(s,						(struct sockaddr *)&sin,						sizeof(struct sockaddr_in),						ld->ld_connect_timeout);				} else {					err = (*connectfn)(s,						(struct sockaddr *)&sin,						sizeof(struct sockaddr_in));				}#ifdef NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED/* * restore original signal mask */				NSLDAPI_MT_SAFE_SIGPROCMASK( SIG_SETMASK, &oldset, 0 );#endif /* NSLDAPI_CONNECT_MUST_NOT_BE_INTERRUPTED */			}			if ( err >= 0 ) {				connected = 1;				rc = 0;				break;			} else {				if ( ld->ld_options & LDAP_BITOPT_ASYNC) {#ifdef _WINDOWS					if (err == -1 && WSAGetLastError() == WSAEWOULDBLOCK)						LDAP_SET_ERRNO( ld, EWOULDBLOCK );#endif /* _WINDOWS */					err = LDAP_GET_ERRNO( ld );					if ( NSLDAPI_ERRNO_IO_INPROGRESS( err )) {						LDAPDebug( LDAP_DEBUG_TRACE, "connect would block...\n",							   0, 0, 0 );						rc = -2;						break;					}				}#ifdef LDAP_DEBUG				if ( ldap_debug & LDAP_DEBUG_TRACE ) {					perror( (char *)inet_ntoa( sin.sin_addr ));				}#endif				(*closefn)( s );				if ( !use_hp ) {					break;				}			}		}		ldap_memfree( host );		parse_err = ldap_x_hostlist_next( &host, &port, status );	}	if ( ldhpbuf_allocd != NULL ) {		NSLDAPI_FREE( ldhpbuf_allocd );	}	ldap_memfree( host );	ldap_x_hostlist_statusfree( status );	if ( connected ) {		LDAPDebug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",		    s, inet_ntoa( sin.sin_addr ), 0 );	}	return( rc == 0 ? s : -1 );#endif /* NSLDAPI_AVOID_OS_SOCKETS */}