/**
  * Connect and bind to the LDAP server.
  *
  * @param mixed $binddn
  *   Use the given DN while binding. Use NULL for an anonymous bind.
  * @param mixed $bindpw
  *   Use the given password while binding. Use NULL for an anonymous bind.
  * @param boolean $rebind
  *   Reset the object's bind credentials to those provided. Otherwise, just
  *   bind to verify that the credentials are valid.
  *
  * @return boolean
  *   TRUE on success, FALSE on failure.
  */
 public function bind($binddn = FALSE, $bindpw = FALSE, $rebind = FALSE)
 {
     // Connect first.
     try {
         $this->connect();
     } catch (SimpleLdapException $e) {
         return FALSE;
     }
     // Reset bind DN if provided, and reset is specified.
     if ($rebind && $binddn !== FALSE && $binddn != $this->binddn) {
         $this->binddn = $binddn;
         $this->bound = FALSE;
     }
     // Reset bind PW if provided, and reset is specified.
     if ($rebind && $bindpw !== FALSE && $bindpw != $this->bindpw) {
         $this->bindpw = $bindpw;
         $this->bound = FALSE;
     }
     // Attempt to bind if not already bound, or rebind is specified, or
     // credentials are given.
     if (!$this->bound || $rebind || $binddn !== FALSE && $bindpw !== FALSE) {
         // Bind to the LDAP server.
         if ($rebind || $binddn === FALSE || $bindpw === FALSE) {
             $this->bound = SimpleLdap::ldap_bind($this->resource, $this->binddn, $this->bindpw);
         } else {
             // Bind with the given credentials. This is a temporary bind to verify
             // the password, so $this->bound is reset to FALSE.
             $result = SimpleLdap::ldap_bind($this->resource, $binddn, $bindpw);
             $this->bound = FALSE;
             return $result;
         }
         // If paged queries are enabled, verify whether the server supports them.
         if ($this->bound && $this->pagesize) {
             // Load the rootDSE.
             $this->rootdse();
             // Look for the paged query OID supported control.
             if (!in_array('1.2.840.113556.1.4.319', $this->rootdse['supportedcontrol'])) {
                 $this->pagesize = FALSE;
             }
         }
     }
     return $this->bound;
 }