'Cleans up the socket.'

def connectionLost(self, reason):

    self._logConnectionLostMsg()
    self._realPortNumber = None
    d = None
    if hasattr(self, 'deferred'):
        d = self.deferred
        del self.deferred
    self.disconnected = True
    self.reactor.removeActiveHandle(self)
    self.connected = False
    self._closeSocket(True)
    del self.socket
    del self.getFileHandle
    try:
        self.factory.doStop()
    except:
        self.disconnecting = False
        if (d is not None):
            d.errback(failure.Failure())
        else:
            raise
    else:
        self.disconnecting = False
        if (d is not None):
            d.callback(None)
