def METHOD_NAME(self, manager, timeout=5):
    elapsed_time = 0
    gateway = None
    self.connected = False
    self.stdout.write('Connecting to Gateway')
    while not self.connected and elapsed_time <= timeout:
        try:
            gateway = manager.get_gateway()
            gateway.ping()
            self.connected = True
        except (multiprocessing.managers.RemoteError, KeyError, AttributeError, BrokenPipeError):
            self.stdout.write('.')
            gateway = manager.get_gateway()
            self.connected = True
            sleep(1)
        except FileNotFoundError:
            self.stdout.write('.')
            sleep(1)
            continue
        finally:
            elapsed_time += 1
    if not self.connected:
        raise RuntimeError('Timeout error')
    self.stdout.write('\n')
    return gateway