'Try to connect to the remote SSH server. If a connection times out or
is refused it is retried up to timeout number of seconds.
:param ssh_client: A configured SSHClient instance
:type ssh_client: ``SSHClient``
:param wait_period: How many seconds to wait between each loop
iteration. (default is 1.5)
:type wait_period: ``int``
:param timeout: How many seconds to wait before giving up.
(default is 300)
:type timeout: ``int``
:return: ``SSHClient`` on success'

def _ssh_client_connect(self, ssh_client, wait_period=1.5, timeout=300):

    start = time.time()
    end = (start + timeout)
    while (time.time() < end):
        try:
            ssh_client.connect()
        except SSH_TIMEOUT_EXCEPTION_CLASSES:
            e = sys.exc_info()[1]
            message = str(e).lower()
            expected_msg = 'no    such    file    or    directory'
            if (isinstance(e, IOError) and (expected_msg in message)):
                raise e
            ssh_client.close()
            time.sleep(wait_period)
            continue
        else:
            return ssh_client
    raise LibcloudError(value=('Could    not    connect    to    the    remote    SSH    ' + 'server.    Giving    up.'), driver=self)
