diff '--exclude=.svn' -ur org/postgresql/core/PGStream.java ../x/org/postgresql/core/PGStream.java
--- org/postgresql/core/PGStream.java	2012-08-21 18:47:10.619742600 -0400
+++ ../x/org/postgresql/core/PGStream.java	2012-08-13 11:34:25.805000000 -0400
@@ -32,6 +32,7 @@
 public class PGStream
 {
     private final InetSocketAddress address;
+    private final String localAddress;
 
     private final byte[] _int4buf;
     private final byte[] _int2buf;
@@ -48,15 +49,19 @@
      * Constructor:  Connect to the PostgreSQL back end and return
      * a stream connection.
      *
-     * @param host the hostname to connect to
-     * @param port the port number that the postmaster is sitting on
+     * @param address the hostname and port of a postmaster to connect to
+     * @param localAddress the local address to bind to (or null for "any local address")
      * @exception IOException if an IOException occurs below it.
      */
-    public PGStream(InetSocketAddress address) throws IOException
+    public PGStream(InetSocketAddress address, String localAddress) throws IOException
     {
         this.address = address;
+        this.localAddress = localAddress;
 
         Socket socket = new Socket();
+        if (localAddress != null) {
+            socket.bind(new InetSocketAddress(localAddress, 0));
+        }
         socket.connect(address);
         changeSocket(socket);
         setEncoding(Encoding.getJVMEncoding("US-ASCII"));
@@ -69,6 +74,10 @@
         return address;
     }
 
+    public String getLocalAddress() {
+        return localAddress;
+    }
+
     public Socket getSocket() {
         return connection;
     }
diff '--exclude=.svn' -ur org/postgresql/core/v2/ConnectionFactoryImpl.java ../x/org/postgresql/core/v2/ConnectionFactoryImpl.java
--- org/postgresql/core/v2/ConnectionFactoryImpl.java	2012-08-21 18:47:09.611742600 -0400
+++ ../x/org/postgresql/core/v2/ConnectionFactoryImpl.java	2012-08-13 11:34:24.708000000 -0400
@@ -83,7 +83,7 @@
         PGStream newStream = null;
         try
         {
-            newStream = new PGStream(address);
+            newStream = new PGStream(address, info.getProperty("localAddress"));
 
             // Construct and send an ssl startup packet if requested.
             if (trySSL)
@@ -199,7 +199,7 @@
 
             // We have to reconnect to continue.
             pgStream.close();
-            return new PGStream(pgStream.getAddress());
+            return new PGStream(pgStream.getAddress(), pgStream.getLocalAddress());
 
         case 'N':
             if (logger.logDebug())
diff '--exclude=.svn' -ur org/postgresql/core/v2/ProtocolConnectionImpl.java ../x/org/postgresql/core/v2/ProtocolConnectionImpl.java
--- org/postgresql/core/v2/ProtocolConnectionImpl.java	2012-08-21 18:47:09.580742600 -0400
+++ ../x/org/postgresql/core/v2/ProtocolConnectionImpl.java	2012-08-13 11:34:24.530000000 -0400
@@ -87,7 +87,7 @@
             if (logger.logDebug())
                 logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")");
 
-            cancelStream = new PGStream(pgStream.getAddress());
+            cancelStream = new PGStream(pgStream.getAddress(), pgStream.getLocalAddress());
             cancelStream.SendInteger4(16);
             cancelStream.SendInteger2(1234);
             cancelStream.SendInteger2(5678);
diff '--exclude=.svn' -ur org/postgresql/core/v3/ConnectionFactoryImpl.java ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java
--- org/postgresql/core/v3/ConnectionFactoryImpl.java	2012-08-21 18:47:09.930742600 -0400
+++ ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java	2012-08-13 11:34:25.267000000 -0400
@@ -98,7 +98,7 @@
         PGStream newStream = null;
         try
         {
-            newStream = new PGStream(address);
+            newStream = new PGStream(address, info.getProperty("localAddress"));
 
             // Construct and send an ssl startup packet if requested.
             if (trySSL)
@@ -257,7 +296,7 @@
 
             // We have to reconnect to continue.
             pgStream.close();
-            return new PGStream(pgStream.getAddress());
+            return new PGStream(pgStream.getAddress(), pgStream.getLocalAddress());
 
         case 'N':
             if (logger.logDebug())
diff '--exclude=.svn' -ur org/postgresql/core/v3/ProtocolConnectionImpl.java ../x/org/postgresql/core/v3/ProtocolConnectionImpl.java
--- org/postgresql/core/v3/ProtocolConnectionImpl.java	2012-08-21 18:47:09.872742600 -0400
+++ ../x/org/postgresql/core/v3/ProtocolConnectionImpl.java	2012-08-13 11:34:25.095000000 -0400
@@ -89,7 +89,7 @@
             if (logger.logDebug())
                 logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")");
 
-            cancelStream = new PGStream(pgStream.getAddress());
+            cancelStream = new PGStream(pgStream.getAddress(), pgStream.getLocalAddress());
             cancelStream.SendInteger4(16);
             cancelStream.SendInteger2(1234);
             cancelStream.SendInteger2(5678);
diff '--exclude=.svn' -ur org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java ../x/org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java
--- org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java	2012-08-21 18:47:12.150742600 -0400
+++ ../x/org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java	2012-08-13 11:34:27.859000000 -0400
@@ -59,7 +59,7 @@
             }
         }
 
-        Socket newConnection = factory.createSocket(stream.getSocket(), stream.getAddress().getHostString(), stream.getAddress().getPort(), true);
+        Socket newConnection = factory.createSocket(stream.getSocket(), stream.getAddress().getHostName(), stream.getAddress().getPort(), true);
         stream.changeSocket(newConnection);
     }
 
