title: ldapjs - write after end error
body: <p>I have created an authentication service using the following code in Node.js and ldapjs. </p>
  
  <pre><code>var when = require ('when');
  var AuthenticationError = require('../errors/AuthenticationError');
  var SessionManagerService = require('./SessionManagerService');
  
  var ldap = require('ldapjs');
  
  var client = ldap.createClient({
      url: 'ldaps://ad.mycompany.com:636',
      tlsOptions: {'rejectUnauthorized': false}
  });
  
  module.exports = {
  
      signIn: function (email, password) {
          return this.ldapBind(email, password).then(
              function () {
                  return SessionManagerService.createSessionHash({email: email});
              }
          );
      },
  
      ldapBind: function (email, password) {
          var deferred = when.defer();
          client.bind(email, password, function(err) {
              if (err) {
                  deferred.reject (new AuthenticationError('Invalid username and/or password!', 'Authentication.signIn.error'));
              } else {
                  client.unbind();
                  deferred.resolve(true);
              }
          });
          return deferred.promise;
      },
  
  };
  </code></pre>
  
  <p>When I authenticate my user for the first time, it works perfectly but it fails starting from the second attempt.
  The error message is: <strong>'write after end'</strong>.</p>
  
  <p>When I restart the node server it works again for the first attempt. </p>
  
  <p>It looks like something is hanging but I don't know what. Any idea?</p>