int	rc;
int		rc = -1;
nsldapi_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,	int connect, int bind ){    int	rc;    	LDAPConn	*lc;	LDAPServer	*prevsrv, *srv;	Sockbuf		*sb = NULL;	/*	 * make a new LDAP server connection	 */	if (( lc = (LDAPConn *)NSLDAPI_CALLOC( 1, sizeof( LDAPConn ))) == NULL	    || ( !use_ldsb && ( sb = ber_sockbuf_alloc()) == NULL )) {		if ( lc != NULL ) {			NSLDAPI_FREE( (char *)lc );		}		LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );		return( NULL );	}	LDAP_MUTEX_LOCK( ld, LDAP_OPTION_LOCK );	if ( !use_ldsb ) {		/*		 * we have allocated a new sockbuf		 * set I/O routines to match those in default LDAP sockbuf		 */		IFP				sb_fn;		struct lber_x_ext_io_fns	extiofns;				extiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;		if ( ber_sockbuf_get_option( ld->ld_sbp,		    LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns ) == 0 ) {			ber_sockbuf_set_option( sb,			    LBER_SOCKBUF_OPT_EXT_IO_FNS, &extiofns );		}		if ( ber_sockbuf_get_option( ld->ld_sbp,		    LBER_SOCKBUF_OPT_READ_FN, (void *)&sb_fn ) == 0		    && sb_fn != NULL ) {			ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_READ_FN,			    (void *)sb_fn );		}		if ( ber_sockbuf_get_option( ld->ld_sbp,		    LBER_SOCKBUF_OPT_WRITE_FN, (void *)&sb_fn ) == 0		    && sb_fn != NULL ) {			ber_sockbuf_set_option( sb, LBER_SOCKBUF_OPT_WRITE_FN,			    (void *)sb_fn );		}	}	lc->lconn_sb = ( use_ldsb ) ? ld->ld_sbp : sb;	lc->lconn_version = ld->ld_version;	/* inherited */	LDAP_MUTEX_UNLOCK( ld, LDAP_OPTION_LOCK );	if ( connect ) {		prevsrv = NULL;        /*          * save the return code for later         */ 		for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {			rc = nsldapi_connect_to_host( ld, lc->lconn_sb,				   srv->lsrv_host, srv->lsrv_port,			       (  srv->lsrv_options & LDAP_SRV_OPT_SECURE ) != 0,					&lc->lconn_krbinstance );			if (rc != -1) {				break;			}			prevsrv = srv;		}		if ( srv == NULL ) {		    if ( !use_ldsb ) {			NSLDAPI_FREE( (char *)lc->lconn_sb );		    }		    NSLDAPI_FREE( (char *)lc );		    /* nsldapi_open_ldap_connection has already set ld_errno */		    return( NULL );		}		if ( prevsrv == NULL ) {		    *srvlistp = srv->lsrv_next;		} else {		    prevsrv->lsrv_next = srv->lsrv_next;		}		lc->lconn_server = srv;	}	if (ld->ld_options & LDAP_BITOPT_ASYNC && rc == -2)    {        lc->lconn_status = LDAP_CONNST_CONNECTING;    }    else {        lc->lconn_status = LDAP_CONNST_CONNECTED;    }    	lc->lconn_next = ld->ld_conns;	ld->ld_conns = lc;	/*	 * XXX for now, we always do a synchronous bind.  This will have	 * to change in the long run...	 */	if ( bind ) {		int		err, lderr, freepasswd, authmethod;		char		*binddn, *passwd;		LDAPConn	*savedefconn;		freepasswd = err = 0;		if ( ld->ld_rebind_fn == NULL ) {			binddn = passwd = "";			authmethod = LDAP_AUTH_SIMPLE;		} else {			if (( lderr = (*ld->ld_rebind_fn)( ld, &binddn, &passwd,			    &authmethod, 0, ld->ld_rebind_arg ))			    == LDAP_SUCCESS ) {				freepasswd = 1;			} else {				LDAP_SET_LDERRNO( ld, lderr, NULL, NULL );				err = -1;			}		}		if ( err == 0 ) {			savedefconn = ld->ld_defconn;			ld->ld_defconn = lc;			++lc->lconn_refcnt;	/* avoid premature free */			/*			 * when binding, we will back down as low as LDAPv2			 * if we get back "protocol error" from bind attempts			 */			for ( ;; ) {				/* LDAP_MUTEX_UNLOCK(ld, LDAP_CONN_LOCK); */				if (( lderr = ldap_bind_s( ld, binddn, passwd,				    authmethod )) == LDAP_SUCCESS ) {					/* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */					break;				}				/* LDAP_MUTEX_LOCK(ld, LDAP_CONN_LOCK); */				if ( lc->lconn_version <= LDAP_VERSION2				    || lderr != LDAP_PROTOCOL_ERROR ) {					err = -1;					break;				}				--lc->lconn_version;	/* try lower version */			}			--lc->lconn_refcnt;			ld->ld_defconn = savedefconn;		}		if ( freepasswd ) {			(*ld->ld_rebind_fn)( ld, &binddn, &passwd,				&authmethod, 1, ld->ld_rebind_arg );		}		if ( err != 0 ) {			nsldapi_free_connection( ld, lc, NULL, NULL, 1, 0 );			lc = NULL;		}	}	return( lc );}