how_to_open_connection(HttpTransact::State* s)
{
  ink_assert(s->pending_work == NULL);

  // Originally we returned which type of server to open
  // Now, however, we may want to issue a cache
  // operation first in order to lock the cache
  // entry to prevent multiple origin server requests
  // for the same document.
  // The cache operation that we actually issue, of
  // course, depends on the specified "cache_action".
  // If there is no cache-action to be issued, just
  // connect to the server.
  switch (s->cache_info.action) {
  case HttpTransact::CACHE_PREPARE_TO_DELETE:
  case HttpTransact::CACHE_PREPARE_TO_UPDATE:
  case HttpTransact::CACHE_PREPARE_TO_WRITE:
    s->transact_return_point = HttpTransact::handle_cache_write_lock;
    return HttpTransact::SM_ACTION_CACHE_ISSUE_WRITE;
  default:
    // This covers:
    // CACHE_DO_UNDEFINED, CACHE_DO_NO_ACTION, CACHE_DO_DELETE,
    // CACHE_DO_LOOKUP, CACHE_DO_REPLACE, CACHE_DO_SERVE,
    // CACHE_DO_SERVE_AND_DELETE, CACHE_DO_SERVE_AND_UPDATE,
    // CACHE_DO_UPDATE, CACHE_DO_WRITE, TOTAL_CACHE_ACTION_TYPES
    break;
  }

  if (s->method == HTTP_WKSIDX_CONNECT && s->parent_result.r != PARENT_SPECIFIED) {
    s->cdn_saved_next_action = HttpTransact::SM_ACTION_ORIGIN_SERVER_RAW_OPEN;
  } else {
    s->cdn_saved_next_action = HttpTransact::SM_ACTION_ORIGIN_SERVER_OPEN;
  }

  // In the following, if url_remap_mode == 2 (URL_REMAP_FOR_OS)
  // then do remapping for requests to OS's.
  // Whether there is CDN remapping or not, goto SM_ACTION_DNS_LOOKUP;
  // after that, it'll goto ORIGIN_SERVER_(RAW_)OPEN as needed.

  if ((url_remap_mode == HttpTransact::URL_REMAP_FOR_OS) &&
      (s->current.request_to == HttpTransact::ORIGIN_SERVER) && !s->cdn_remap_complete) {
    DebugTxn("cdn", "*** START CDN Remapping *** CDN mode = %d", url_remap_mode);

    char *remap_redirect = NULL;
    int host_len;
    const char *host;

    // We need to copy the client request into the server request.  Why?  BUGBUG
    s->hdr_info.server_request.url_set(s->hdr_info.client_request.url_get());

    // TODO yeah, not sure everything here is correct with redirects
    // This probably doesn't work properly, since request_url_remap() is broken.
    if (request_url_remap(s, &s->hdr_info.server_request, &remap_redirect)) {
      ink_assert(!remap_redirect);      // should not redirect in this code
      HttpTransact::initialize_state_variables_for_origin_server(s, &s->hdr_info.server_request, true);
      DebugTxn("cdn", "Converting proxy request to server request");
      // Check whether a Host header field is missing from a 1.0 or 1.1 request.
      if (                      /*outgoing_version != HTTPVersion(0,9) && */
           !s->hdr_info.server_request.presence(MIME_PRESENCE_HOST)) {
        URL *url = s->hdr_info.server_request.url_get();
        host = url->host_get(&host_len);
        // Add a ':port' to the HOST header if the request is not going
        // to the default port.
        int port = url->port_get();
        if (port != url_canonicalize_port(URL_TYPE_HTTP, 0)) {
          char *buf = (char *)alloca(host_len + 15);
          memcpy(buf, host, host_len);
          host_len += snprintf(buf + host_len, 15, ":%d", port);
          s->hdr_info.server_request.value_set(MIME_FIELD_HOST, MIME_LEN_HOST, buf, host_len);
        } else {
          s->hdr_info.server_request.value_set(MIME_FIELD_HOST, MIME_LEN_HOST, host, host_len);
        }
        ats_free(remap_redirect);  // This apparently shouldn't happen...
      }
      // Stripping out the host name from the URL
      if (s->current.server == &s->server_info && s->next_hop_scheme == URL_WKSIDX_HTTP) {
        DebugTxn("cdn", "Removing host name from URL");
        HttpTransactHeaders::remove_host_name_from_url(&s->hdr_info.server_request);
      }
    }                           // the URL was remapped
    if (is_debug_tag_set("cdn")) {
      char *d_url = s->hdr_info.server_request.url_get()->string_get(NULL);
      if (d_url)
        DebugTxn("cdn", "URL: %s", d_url);
      char *d_hst = (char *) s->hdr_info.server_request.value_get(MIME_FIELD_HOST, MIME_LEN_HOST, &host_len);
      if (d_hst)
        DebugTxn("cdn", "Host Hdr: %s", d_hst);
      ats_free(d_url);
    }
    s->cdn_remap_complete = true;       // It doesn't matter if there was an actual remap or not
    s->transact_return_point = HttpTransact::OSDNSLookup;
    ink_assert(s->next_action);
    ink_assert(s->cdn_saved_next_action);
    return HttpTransact::SM_ACTION_DNS_LOOKUP;
  }

  if (!s->already_downgraded) { //false unless downgraded previously (possibly due to HTTP 505)
    (&s->hdr_info.server_request)->version_set(HTTPVersion(1, 1));
    HttpTransactHeaders::convert_request(s->current.server->http_version, &s->hdr_info.server_request);
  }

  ink_assert(s->cdn_saved_next_action == HttpTransact::SM_ACTION_ORIGIN_SERVER_OPEN ||
                    s->cdn_saved_next_action == HttpTransact::SM_ACTION_ORIGIN_SERVER_RAW_OPEN);
  return s->cdn_saved_next_action;
}