Refactor LDAP sync to avoid multiple connections and recursive calls