--- src/ident/Ident.cc.orig	2012-12-10 04:53:26.000000000 -0500
+++ src/ident/Ident.cc	2012-12-24 22:41:00.000000000 -0500
@@ -72,18 +72,19 @@
 static CTCB Timeout;
 static CNCB ConnectDone;
 static hash_table *ident_hash = NULL;
 static void ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data);
 static void identCallback(IdentStateData * state, char *result);
+static void Close_(IdentStateData * state);
 
 } // namespace Ident
 
 Ident::IdentConfig Ident::TheConfig;
 
 /**** PRIVATE FUNCTIONS ****/
 
-void
+static void
 Ident::identCallback(IdentStateData * state, char *result)
 {
     IdentClient *client;
 
     if (result && *result == '\0')
@@ -98,22 +99,28 @@
 
         xfree(client);
     }
 }
 
-void
-Ident::Close(const CommCloseCbParams &params)
+static void
+Ident::Close_(IdentStateData * state)
 {
-    IdentStateData *state = (IdentStateData *)params.data;
     identCallback(state, NULL);
     state->conn->close();
+    state->conn = NULL;
     hash_remove_link(ident_hash, (hash_link *) state);
     xfree(state->hash.key);
     cbdataFree(state);
 }
 
 void
+Ident::Close(const CommCloseCbParams &params)
+{
+    Ident::Close_((IdentStateData *)params.data);
+}
+
+void
 Ident::Timeout(const CommTimeoutCbParams &io)
 {
     debugs(30, 3, HERE << io.conn);
     io.conn->close();
 }
@@ -121,18 +128,21 @@
 void
 Ident::ConnectDone(const Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data)
 {
     IdentStateData *state = (IdentStateData *)data;
 
+    assert(conn != NULL && conn == state->conn);
+
     if (status != COMM_OK) {
         if (status == COMM_TIMEOUT) {
             debugs(30, 3, "IDENT connection timeout to " << state->conn->remote);
         }
+        Ident::Close_(state);
         return;
     }
 
-    assert(conn != NULL && conn == state->conn);
+    comm_add_close_handler(conn->fd, Ident::Close, state);
 
     /*
      * see if any of our clients still care
      */
     IdentClient *c;
@@ -141,16 +151,14 @@
             break;
     }
 
     if (c == NULL) {
         /* no clients care */
-        conn->close();
+        state->conn->close();
         return;
     }
 
-    comm_add_close_handler(conn->fd, Ident::Close, state);
-
     MemBuf mb;
     mb.init();
     mb.Printf("%d, %d\r\n",
               conn->remote.GetPort(),
               conn->local.GetPort());
@@ -215,11 +223,11 @@
     }
 
     state->conn->close();
 }
 
-void
+static void
 Ident::ClientAdd(IdentStateData * state, IDCB * callback, void *callback_data)
 {
     IdentClient *c = (IdentClient *)xcalloc(1, sizeof(*c));
     IdentClient **C;
     c->callback = callback;
