Index: docs/libcurl/curl_easy_setopt.3
===================================================================
RCS file: /cvsroot/curl/curl/docs/libcurl/curl_easy_setopt.3,v
retrieving revision 1.226
diff -u -r1.226 curl_easy_setopt.3
--- docs/libcurl/curl_easy_setopt.3	30 Jul 2008 21:55:26 -0000	1.226
+++ docs/libcurl/curl_easy_setopt.3	5 Aug 2008 12:55:04 -0000
@@ -237,6 +237,15 @@
 Pass a pointer that will be untouched by libcurl and passed as the first
 argument in the opensocket callback set with \fICURLOPT_OPENSOCKETFUNCTION\fP.
 (Option added in 7.17.1.)
+.IP CURLOPT_CONNECTFUNCTION
+Function pointer that should match the \fIcurl_connect_callback\fP
+prototype found in \fI<curl/curl.h>\fP. This function gets called by libcurl
+instead of the \fIconnect(2)\fP call. The callback function should return
+0 on success or -1 on error, and ideally also update \fIerrno\fP.
+The default behavior is the equivalent of:
+.Bd -literal -offset indent
+  return connect(sockfd, &addr->addr, addr->addrlen);
+.Ed
 .IP CURLOPT_PROGRESSFUNCTION
 Function pointer that should match the \fIcurl_progress_callback\fP prototype
 found in \fI<curl/curl.h>\fP. This function gets called by libcurl instead of
Index: include/curl/curl.h
===================================================================
RCS file: /cvsroot/curl/curl/include/curl/curl.h,v
retrieving revision 1.360
diff -u -r1.360 curl.h
--- include/curl/curl.h	30 Jul 2008 21:55:26 -0000	1.360
+++ include/curl/curl.h	5 Aug 2008 12:55:04 -0000
@@ -287,6 +287,10 @@
                             curlsocktype purpose,
                             struct curl_sockaddr *address);
 
+typedef int (*curl_connect_callback)(int sockfd, 
+				     const struct sockaddr *serv_addr,
+				     socklen_t addrlen);
+				      
 #ifndef CURL_NO_OLDIES
   /* not used since 7.10.8, will be removed in a future release */
 typedef int (*curl_passwd_callback)(void *clientp,
@@ -1214,6 +1218,10 @@
   /* (IPv6) Address scope */
   CINIT(ADDRESS_SCOPE, LONG, 171),
 
+  /* Callback function for connecting a socket (instead of connect(2)).
+     curl_connect_callback */
+  CINIT(CONNECTFUNCTION, FUNCTIONPOINT, 172),
+
   CURLOPT_LASTENTRY /* the last unused */
 } CURLoption;
 
@@ -1243,8 +1251,8 @@
   /* Below here follows defines for the CURLOPT_IPRESOLVE option. If a host
      name resolves addresses using more than one IP protocol version, this
      option might be handy to force libcurl to use a specific IP version. */
-#define CURL_IPRESOLVE_WHATEVER 0 /* default, resolves addresses to all IP
-                                     versions that your system allows */
+#define CURL_IPRESOLVE_WHATEVER 0 /* default, resolves addresses to all IP 
+				     versions that your system allows */
 #define CURL_IPRESOLVE_V4       1 /* resolve to ipv4 addresses */
 #define CURL_IPRESOLVE_V6       2 /* resolve to ipv6 addresses */
 
Index: lib/connect.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/connect.c,v
retrieving revision 1.196
diff -u -r1.196 connect.c
--- lib/connect.c	30 Jul 2008 21:55:27 -0000	1.196
+++ lib/connect.c	5 Aug 2008 12:55:04 -0000
@@ -833,8 +833,12 @@
   Curl_nonblock(sockfd, TRUE);
 
   /* Connect TCP sockets, bind UDP */
-  if(conn->socktype == SOCK_STREAM)
-    rc = connect(sockfd, &addr->addr, addr->addrlen);
+  if(conn->socktype == SOCK_STREAM) {
+    if (data->set.fconnect)
+      rc = data->set.fconnect(sockfd, &addr->addr, addr->addrlen);
+    else
+      rc = connect(sockfd, &addr->addr, addr->addrlen);
+  }
   else
     rc = 0;
 
Index: lib/easy.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/easy.c,v
retrieving revision 1.120
diff -u -r1.120 easy.c
--- lib/easy.c	12 May 2008 21:43:29 -0000	1.120
+++ lib/easy.c	5 Aug 2008 12:55:04 -0000
@@ -767,6 +767,8 @@
                                                       type */
   data->set.new_file_perms = 0644;    /* Default permissions */
   data->set.new_directory_perms = 0755; /* Default permissions */
+
+  data->set.fconnect = (curl_connect_callback) connect;
 }
 
 /*
Index: lib/url.c
===================================================================
RCS file: /cvsroot/curl/curl/lib/url.c,v
retrieving revision 1.726
diff -u -r1.726 url.c
--- lib/url.c	1 Aug 2008 02:09:08 -0000	1.726
+++ lib/url.c	5 Aug 2008 12:55:04 -0000
@@ -2100,6 +2100,16 @@
     data->set.scope = (unsigned int) va_arg(param, long);
     break;
 
+  case CURLOPT_CONNECTFUNCTION:
+    /*
+     * Connect callback function: called instead of connect(2)
+     */
+    data->set.fconnect = va_arg(param, curl_connect_callback);
+    if (!data->set.fconnect) {
+	data->set.fconnect = (curl_connect_callback)connect;
+    }
+    break;
+
   default:
     /* unknown tag and its companion, just ignore: */
     result = CURLE_FAILED_INIT; /* correct this */
Index: lib/urldata.h
===================================================================
RCS file: /cvsroot/curl/curl/lib/urldata.h,v
retrieving revision 1.384
diff -u -r1.384 urldata.h
--- lib/urldata.h	30 Jul 2008 21:55:27 -0000	1.384
+++ lib/urldata.h	5 Aug 2008 12:55:04 -0000
@@ -1368,6 +1368,8 @@
   void *sockopt_client; /* pointer to pass to the socket options callback */
   curl_opensocket_callback fopensocket; /* function for checking/translating
                                            the address and opening the socket */
+  curl_connect_callback fconnect; /* function to use instead of connect(2) 
+				     for connecting a socket */
   void* opensocket_client;
 
   void *seek_client;    /* pointer to pass to the seek callback */
