--- work/others/jungerl/lib/eldap/src/eldap.erl	2005-03-01 00:29:22.000000000 +0100
+++ work/su.se/official-yxa/src/eldap/eldap.erl	2006-11-24 10:08:27.000000000 +0100
@@ -366,15 +366,7 @@
 	    ssl:seed("bkrlnateqqo" ++ integer_to_list(X));
        true -> true
     end,
-    {ok, Fd} = ssl:connect(Host, Data#eldap.port, [{verify,0}|Opts]),
-    if Vsn >= "5.3" ->
-	    %% In R9C, but not in R9B
-	    {ok, Cert} = ssl:peercert(Fd, [ssl, subject]),
-	    io:fwrite("ssl_connect: peer cert:~n~p~n", [Cert]);
-       true ->
-	    io:fwrite("ssl-connect succeded~n", [])
-    end,
-    {ok, Fd}.
+    ssl:connect(Host, Data#eldap.port, [{verify,0}|Opts]).
 
 
 loop(Cpid, Data) ->
@@ -468,7 +460,7 @@
   Msg#'LDAPMessage'.messageID == Data#eldap.id ->
     case Msg#'LDAPMessage'.protocolOp of
 	{bindResponse, Result} ->
-	    case Result#'LDAPResult'.resultCode of
+	    case Result#'BindResponse'.resultCode of
 		success -> {ok,Data};
 		Error   -> {error, Error}
 	    end;
@@ -785,7 +777,20 @@
 send(To,Msg) -> To ! {self(),Msg}.
 recv(From)   -> receive {From,Msg} -> Msg end.
 
+ldap_closed_p(Data, Emsg) when Data#eldap.use_tls == true ->
+    %% Check if the SSL socket seems to be alive or not
+    case catch ssl:sockname(Data#eldap.fd) of
+	{error, _} ->
+	    ssl:close(Data#eldap.fd),
+	    {error, ldap_closed};
+	{ok, _} ->
+	    {error, Emsg};
+	_ ->
+	    %% sockname crashes if the socket pid is not alive
+	    {error, ldap_closed}
+    end;
 ldap_closed_p(Data, Emsg) ->
+    %% non-SSL socket
     case inet:port(Data#eldap.fd) of
 	{error,_} -> {error, ldap_closed};
 	_         -> {error,Emsg}
