'Reset the stream\'s state and reconnect to the server.'

def reconnect(self, reattempt=True, wait=False, send_close=True):

    log.debug(u'reconnecting...')
    if self.state.ensure(u'connected'):
        self.state.transition(u'connected', u'disconnected', wait=2.0, func=self._disconnect, args=(True, wait, send_close))
    attempts = self.reconnect_max_attempts
    log.debug(u'connecting...')
    connected = self.state.transition(u'disconnected', u'connected', wait=2.0, func=self._connect, args=(reattempt,))
    while (reattempt and (not connected) and (not self.stop.is_set())):
        connected = self.state.transition(u'disconnected', u'connected', wait=2.0, func=self._connect)
        connected = (connected or self.state.ensure(u'connected'))
        if (not connected):
            if (attempts is not None):
                attempts -= 1
                if (attempts <= 0):
                    self.event(u'connection_failed', direct=True)
                    return False
    return connected
