Index: org/postgresql/core/v2/ProtocolConnectionImpl.java
===================================================================
--- org/postgresql/core/v2/ProtocolConnectionImpl.java	(revision 15188)
+++ org/postgresql/core/v2/ProtocolConnectionImpl.java	(revision 15200)
@@ -90,7 +90,7 @@
             if (logger.logDebug())
                 logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")");
 
-            cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort());
+            cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress());
             cancelStream.SendInteger4(16);
             cancelStream.SendInteger2(1234);
             cancelStream.SendInteger2(5678);
Index: org/postgresql/core/v2/ConnectionFactoryImpl.java
===================================================================
--- org/postgresql/core/v2/ConnectionFactoryImpl.java	(revision 15188)
+++ org/postgresql/core/v2/ConnectionFactoryImpl.java	(revision 15200)
@@ -59,7 +59,7 @@
         PGStream newStream = null;
         try
         {
-            newStream = new PGStream(host, port);
+            newStream = new PGStream(host, port, info.getProperty("localAddress"));
 
             // Construct and send an ssl startup packet if requested.
             if (trySSL)
@@ -160,7 +160,7 @@
 
             // We have to reconnect to continue.
             pgStream.close();
-            return new PGStream(pgStream.getHost(), pgStream.getPort());
+            return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress());
 
         case 'N':
             if (logger.logDebug())
Index: org/postgresql/core/v3/ProtocolConnectionImpl.java
===================================================================
--- org/postgresql/core/v3/ProtocolConnectionImpl.java	(revision 15188)
+++ org/postgresql/core/v3/ProtocolConnectionImpl.java	(revision 15200)
@@ -13,6 +13,7 @@
 import org.postgresql.PGNotification;
 import org.postgresql.core.*;
 
+import java.net.InetAddress;
 import java.sql.*;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -42,6 +43,10 @@
         return pgStream.getPort();
     }
 
+    public String getLocalAddress() {
+        return pgStream.getLocalAddress();
+    }
+
     public String getUser() {
         return user;
     }
@@ -90,7 +95,7 @@
             if (logger.logDebug())
                 logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")");
 
-            cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort());
+            cancelStream = new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress());
             cancelStream.SendInteger4(16);
             cancelStream.SendInteger2(1234);
             cancelStream.SendInteger2(5678);
Index: org/postgresql/core/v3/ConnectionFactoryImpl.java
===================================================================
--- org/postgresql/core/v3/ConnectionFactoryImpl.java	(revision 15188)
+++ org/postgresql/core/v3/ConnectionFactoryImpl.java	(revision 15200)
@@ -73,7 +73,7 @@
         PGStream newStream = null;
         try
         {
-            newStream = new PGStream(host, port);
+            newStream = new PGStream(host, port, info.getProperty("localAddress"));
 
             // Construct and send an ssl startup packet if requested.
             if (trySSL)
@@ -189,7 +189,7 @@
 
             // We have to reconnect to continue.
             pgStream.close();
-            return new PGStream(pgStream.getHost(), pgStream.getPort());
+            return new PGStream(pgStream.getHost(), pgStream.getPort(), pgStream.getLocalAddress());
 
         case 'N':
             if (logger.logDebug())
Index: org/postgresql/core/PGStream.java
===================================================================
--- org/postgresql/core/PGStream.java	(revision 15188)
+++ org/postgresql/core/PGStream.java	(revision 15200)
@@ -16,6 +16,8 @@
 import java.io.IOException;
 import java.io.EOFException;
 import java.io.Writer;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.Socket;
 import java.sql.*;
 
@@ -34,6 +36,7 @@
 {
     private final String host;
     private final int port;
+    private final String localAddress;
 
     private final byte[] _int4buf;
     private final byte[] _int2buf;
@@ -52,14 +55,22 @@
      *
      * @param host the hostname to connect to
      * @param port the port number that the postmaster is sitting on
+     * @param localAddress the local address to bind to (or null for "any local address")
      * @exception IOException if an IOException occurs below it.
      */
-    public PGStream(String host, int port) throws IOException
+    public PGStream(String host, int port, String localAddress) throws IOException
     {
         this.host = host;
         this.port = port;
+        this.localAddress = localAddress;
 
-        changeSocket(new Socket(host, port));
+        if (localAddress == null) {
+            changeSocket(new Socket(host, port));
+        }
+        else {
+            InetAddress addr = InetAddress.getByName(localAddress);
+            changeSocket(new Socket(host, port, addr, 0));
+        }
         setEncoding(Encoding.getJVMEncoding("US-ASCII"));
 
         _int2buf = new byte[2];
@@ -74,6 +85,10 @@
         return port;
     }
 
+    public String getLocalAddress() {
+        return localAddress;
+    }
+
     public Socket getSocket() {
         return connection;
     }
