Adds an optional source_address parameter to socket.create_connection().

For use by issue3972.
diff --git a/Lib/socket.py b/Lib/socket.py
index b379144..9b4beda 100644
--- a/Lib/socket.py
+++ b/Lib/socket.py
@@ -24,7 +24,8 @@
 ssl() -- secure socket layer support (only available if configured)
 socket.getdefaulttimeout() -- get the default timeout value
 socket.setdefaulttimeout() -- set the default timeout value
-create_connection() -- connects to an address, with an optional timeout
+create_connection() -- connects to an address, with an optional timeout and
+                       optional source address.
 
  [*] not available on all platforms!
 
@@ -531,7 +532,8 @@
 
 _GLOBAL_DEFAULT_TIMEOUT = object()
 
-def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
+def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
+                      source_address=None):
     """Connect to *address* and return the socket object.
 
     Convenience function.  Connect to *address* (a 2-tuple ``(host,
@@ -539,7 +541,9 @@
     *timeout* parameter will set the timeout on the socket instance
     before attempting to connect.  If no *timeout* is supplied, the
     global default timeout setting returned by :func:`getdefaulttimeout`
-    is used.
+    is used.  If *source_address* is set it must be a tuple of (host, port)
+    for the socket to bind as a source address before making the connection.
+    An host of '' or port 0 tells the OS to use the default.
     """
 
     msg = "getaddrinfo returns an empty list"
@@ -551,6 +555,8 @@
             sock = socket(af, socktype, proto)
             if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
                 sock.settimeout(timeout)
+            if source_address:
+                sock.bind(source_address)
             sock.connect(sa)
             return sock
 
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 472f403..055af67 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -995,7 +995,7 @@
         ThreadableTest.__init__(self)
 
     def clientSetUp(self):
-        pass
+        self.source_port = test_support.find_unused_port()
 
     def clientTearDown(self):
         self.cli.close()
@@ -1010,6 +1010,19 @@
         self.cli = socket.create_connection((HOST, self.port), timeout=30)
         self.assertEqual(self.cli.family, 2)
 
+    testSourcePort = _justAccept
+    def _testSourcePort(self):
+        self.cli = socket.create_connection((HOST, self.port), timeout=30,
+                source_address=('', self.source_port))
+        self.assertEqual(self.cli.getsockname()[1], self.source_port)
+
+    testSourceAddress = _justAccept
+    def _testSourceAddress(self):
+        self.cli = socket.create_connection(
+                (HOST, self.port), 30, ('127.0.0.1', self.source_port))
+        self.assertEqual(self.cli.getsockname(),
+                         ('127.0.0.1', self.source_port))
+
     testTimeoutDefault = _justAccept
     def _testTimeoutDefault(self):
         # passing no explicit timeout uses socket's global default