Connect to the websocket, retrying as needed. Returns the socket.

def _connect(self, host, port, proc, timeout_seconds):
    """Connect to the websocket, retrying as needed. Returns the socket."""
    if ":" in host and not host.startswith("["):  # Support ipv6 addresses.
      host = "[%s]" % host
    url = "ws://%s:%s/sc2api" % (host, port)

    was_running = False
    for i in range(timeout_seconds):
      is_running = proc and proc.running
      was_running = was_running or is_running
      if (i >= timeout_seconds // 4 or was_running) and not is_running:
        logging.warning(
            "SC2 isn't running, so bailing early on the websocket connection.")
        break
      logging.info("Connecting to: %s, attempt: %s, running: %s", url, i,
                   is_running)
      try:
        return websocket.create_connection(url, timeout=timeout_seconds)
      except socket.error:
        pass  # SC2 hasn't started listening yet.
      except websocket.WebSocketBadStatusException as err:
        if err.status_code == 404:
          pass  # SC2 is listening, but hasn't set up the /sc2api endpoint yet.
        else:
          raise
      time.sleep(1)
    raise ConnectError("Failed to connect to the SC2 websocket. Is it up?")