<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def connectionLost(self, reason=protocol.connectionDone):
 """ Check whether termination was intended and invoke the deferred. If the connection terminated unexpectedly, reraise the failure. @type reason: L{twisted.python.failure.Failure} """

        self.connected = 0

        for pending in self._queue.values():
            pending.errback(reason)
        self._queue.clear()

        if self._stopped:
            result = self if reason.check(error.ConnectionDone) else reason
            self._stopped.callback(result)
            self._stopped = None
        else:
            reason.raiseException()