Upgraded to @gambogi's new CSHLDAP library (using my changes) and greatly simplified this code.