<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def connection(self, handshake=None):
 """ Connects if necessary, returns existing one if it can. :param handshake: A function to be called with the client to complete the handshake. :returns: thrift connection, deferred if necessary """

        if self._state == _State.CONNECTED:
            return succeed(self._current_client)
        elif self._state == _State.DISCONNECTING:
            return fail(ClientDisconnecting())
        elif self._state == _State.NOT_CONNECTED:
            d = self._notify_on_connect()
            self._connect(handshake)
            return d
        else:
            assert self._state == _State.CONNECTING
            return self._notify_on_connect()