import socket
import logging
import time


def try_call(call):
    """Tries to call function several times retrying on socked.timeout."""
    c = 0
    while True:
        try:
            return call()
        except socket.timeout as e:
            c += 1
            if c > 5:
                logging.error('Connection to Pharicator failed, giving up: {}'.format(e))
                raise
            logging.error('Connection to Pharicator failed, retrying: {}'.format(e))
            time.sleep(c * 10)