protected function ldapConnect()
    {

        // try to connect to the LDAP server
        if ($ldapConnection = ldap_connect($this->ldapUrl, $this->ldapPort)) {
            // query whether or no we want to use START TLS
            if ($this->ldapStartTls) {
                ldap_start_tls($ldapConnection);
            }

            // set the LDAP protocol version
            ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);

            // query whether or not we want a anonymous login
            if ($this->allowEmptyPasswords) {
                $bind = ldap_bind($ldapConnection);
            } else {
                $bind = ldap_bind($ldapConnection, $this->bindDN, $this->bindCredential);
            }

            // query whether or not the bind succeeded
            if ($bind) {
                return $ldapConnection;
            }

            // throw an exception if we can't bind using the DN
            throw new LoginException(
                sprintf(
                    'Can\'t bind %s on LDAP server %s:%d',
                    $this->bindDN,
                    $this->ldapUrl,
                    $this->ldapPort
                )
            );
        }

        // throw an exception if we can't connect
        throw new LoginException(
            sprintf(
                'Can\'t connect to the LDAP server %s:%d',
                $this->ldapUrl,
                $this->ldapPort
            )
        );
    }