diff --git a/common/http.c b/common/http.c
index c12bd2b..3d91c19 100644
--- a/common/http.c
+++ b/common/http.c
@@ -1,6 +1,6 @@
 /* http.c  -  HTTP protocol handler
  * Copyright (C) 1999, 2001, 2002, 2003, 2004, 2006,
- *               2009 Free Software Foundation, Inc.
+ *               2009, 2012 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
@@ -134,7 +134,8 @@ static char *build_rel_path (parsed_uri_t uri);
 static gpg_error_t parse_response (http_t hd);
 
 static int connect_server (const char *server, unsigned short port,
-                           unsigned int flags, const char *srvtag);
+                           unsigned int flags, const char *srvtag,
+			   int *did_srv);
 static gpg_error_t write_server (int sock, const char *data, size_t length);
 
 #ifdef HTTP_USE_ESTREAM
@@ -313,7 +314,8 @@ http_register_tls_callback ( gpg_error_t (*cb) (http_t, void *, int) )
 
 /* Start a HTTP retrieval and return on success in R_HD a context
    pointer for completing the the request and to wait for the
-   response. */
+   response.  Note that if srvtag is provided, the host and port part
+   of the URL may be overridden. */
 gpg_error_t
 http_open (http_t *r_hd, http_req_t reqtype, const char *url, 
            const char *auth, unsigned int flags, const char *proxy,
@@ -846,7 +848,7 @@ send_request (http_t hd, const char *auth,
   const char *http_proxy = NULL;
   char *proxy_authstr = NULL;
   char *authstr = NULL;
-  int save_errno;
+  int save_errno,did_srv=0;
 
   tls_session = hd->tls_context;
   if (hd->uri->use_tls && !tls_session)
@@ -894,13 +896,13 @@ send_request (http_t hd, const char *auth,
 
       hd->sock = connect_server (*uri->host ? uri->host : "localhost",
 				 uri->port ? uri->port : 80,
-                                 hd->flags, srvtag);
+                                 hd->flags, srvtag, &did_srv);
       save_errno = errno;
       http_release_parsed_uri (uri);
     }
   else
     {
-      hd->sock = connect_server (server, port, hd->flags, srvtag);
+      hd->sock = connect_server (server, port, hd->flags, srvtag, &did_srv);
       save_errno = errno;
     }
 
@@ -1007,8 +1009,8 @@ send_request (http_t hd, const char *auth,
   else
     {
       char portstr[35];
-        
-      if (port == 80)
+
+      if (port == 80 || did_srv)
         *portstr = 0;
       else
         sprintf (portstr, ":%u", port);
@@ -1540,7 +1542,8 @@ start_server ()
    error.  ERRNO is set on error. */
 static int
 connect_server (const char *server, unsigned short port,
-                unsigned int flags, const char *srvtag)
+                unsigned int flags, const char *srvtag,
+		int *did_srv)
 {
   int sock = -1;
   int srvcount = 0;
@@ -1601,7 +1604,9 @@ connect_server (const char *server, unsigned short port,
     }
 #endif /*USE_DNS_SRV*/
 
-  if (!serverlist)
+  if (serverlist)
+    *did_srv = 1;
+  else
     {
       /* Either we're not using SRV, or the SRV lookup failed.  Make
 	 up a fake SRV record. */
@@ -1621,7 +1626,7 @@ connect_server (const char *server, unsigned short port,
       struct addrinfo hints, *res, *ai;
       char portstr[35];
 
-      sprintf (portstr, "%hu", port);
+      sprintf (portstr, "%hu", serverlist[srv].port);
       memset (&hints, 0, sizeof (hints));
       hints.ai_socktype = SOCK_STREAM;
       if (getaddrinfo (serverlist[srv].target, portstr, &hints, &res))
