Index: usocket.lisp
===================================================================
--- usocket.lisp	(revision 362)
+++ usocket.lisp	(working copy)
@@ -72,14 +72,14 @@
 (defun stream-server-usocket-p (socket)
   (typep socket 'stream-server-usocket))
 
-(defun datagram-usocket-p (socket)
-  (typep socket 'datagram-usocket))
-
 (defclass datagram-usocket (usocket)
   ((connected-p :initarg :connected-p :accessor connected-p))
 ;; ###FIXME: documentation to be added.
   (:documentation ""))
 
+(defun datagram-usocket-p (socket)
+  (typep socket 'datagram-usocket))
+
 (defun make-socket (&key socket)
   "Create a usocket socket type from implementation specific socket."
   (unless socket
@@ -380,7 +380,9 @@
                     ip
                   (get-random-host-by-name host))))
       ((vector t 4) host)
-      (integer (hbo-to-vector-quad host))))
+      ((simple-array (unsigned-byte 8) (4)) host)
+      (integer (hbo-to-vector-quad host))
+      (null nil)))
 
   (defun host-to-hbo (host)
     (etypecase host
@@ -390,7 +392,9 @@
                     (host-byte-order ip)
             (host-to-hbo (get-host-by-name host)))))
       ((vector t 4) (host-byte-order host))
-      (integer host))))
+      ((simple-array (unsigned-byte 8) (4)) (host-byte-order host))
+      (integer host)
+      (null nil))))
 
 ;;
 ;; Other utility functions
Index: backend/lispworks.lisp
===================================================================
--- backend/lispworks.lisp	(revision 362)
+++ backend/lispworks.lisp	(working copy)
@@ -73,7 +73,8 @@
                     (declare (ignore host port err-msg))
                     (raise-usock-err errno socket condition)))))
 
-(defun socket-connect (host port &key (element-type 'base-char) timeout)
+(defun socket-connect (host port &key (element-type 'base-char) timeout
+		                      local-host local-port)
   (when timeout
     (warn "SOCKET-CONNECT timeout not supported in Lispworks"))
   (let ((hostname (host-to-hostname host))
@@ -81,7 +82,9 @@
     (setf stream
           (with-mapped-conditions ()
              (comm:open-tcp-stream hostname port
-                                   :element-type element-type)))
+                                   :element-type element-type
+				   :local-address local-host
+				   :local-port local-port)))
     (if stream
         (make-stream-socket :socket (comm:socket-stream-socket stream)
                             :stream stream)
Index: backend/openmcl.lisp
===================================================================
--- backend/openmcl.lisp	(revision 362)
+++ backend/openmcl.lisp	(working copy)
@@ -70,11 +70,14 @@
       :text
     :binary))
 
-(defun socket-connect (host port &key (element-type 'character) timeout deadline)
+(defun socket-connect (host port &key (element-type 'character) timeout deadline
+		                      local-host local-port)
   (with-mapped-conditions ()
     (let ((mcl-sock
            (openmcl-socket:make-socket :remote-host (host-to-hostname host)
                                        :remote-port port
+				       :local-host local-host
+				       :local-port local-port
                                        :format (to-format element-type)
                                        :deadline deadline
                                        :connect-timeout (and timeout
Index: backend/cmucl.lisp
===================================================================
--- backend/cmucl.lisp	(revision 362)
+++ backend/cmucl.lisp	(working copy)
@@ -3,6 +3,76 @@
 
 ;;;; See LICENSE for licensing information.
 
+(in-package :extensions)
+
+(ext:without-package-locks
+
+;;; Added by Chun Tian (binghe)
+(defun bind-inet-socket (socket host port)
+  "bind a socket to (local) host/port"
+  (with-alien ((sockaddr inet-sockaddr))
+    (setf (slot sockaddr 'family) af-inet)
+    (setf (slot sockaddr 'port) (htons port))
+    (setf (slot sockaddr 'addr) (htonl host))
+    (when (minusp (unix:unix-bind socket
+				  (alien-sap sockaddr)
+				  (alien-size inet-sockaddr :bytes)))
+      (let ((errno (unix:unix-errno)))
+	(unix:unix-close socket)
+	(error 'socket-error
+	       :format-control "Error binding socket to port ~A: ~A"
+	       :format-arguments (list port
+				       (unix:get-unix-error-msg))
+	       :errno errno)))))
+
+(defun connect-to-inet-socket (host port &optional (kind :stream)
+			       &key local-host local-port)
+  "The host may be an address string or an IP address in host order."
+  (let* ((addr (if (stringp host)
+		   (host-entry-addr (or (lookup-host-entry host)
+					(error "Unknown host: ~S." host)))
+		   host))
+	 (socket (create-inet-socket kind)))
+    ;; Added by Chun Tian (binghe)
+    (when (and local-host local-port)
+      (bind-inet-socket socket local-host local-port))
+    (with-alien ((sockaddr inet-sockaddr))
+      (setf (slot sockaddr 'family) af-inet)
+      (setf (slot sockaddr 'port) (htons port))
+      (setf (slot sockaddr 'addr) (htonl addr))
+      (when (minusp (unix:unix-connect socket
+				       (alien-sap sockaddr)
+				       (alien-size inet-sockaddr :bytes)))
+        ;; unix-close may affect errno; save the current value(s)
+        (let ((errno (unix:unix-errno))
+              (errmsg (unix:get-unix-error-msg)))
+          (unix:unix-close socket)
+          (error 'socket-error
+                 :format-control "Error connecting socket to [~A:~A]: ~A"
+                 :format-arguments (list (if (stringp host)
+                                             host
+					     (ip-string addr))
+                                         port
+                                         errmsg)
+                 :errno errno)))
+      socket)))
+
+(defun create-inet-socket (&optional (kind :stream) &key local-host local-port)
+  (multiple-value-bind (proto type)
+		       (internet-protocol kind)
+    (let ((socket (unix:unix-socket af-inet type proto)))
+      (when (minusp socket)
+	(error 'socket-error
+	       :format-control"Error creating socket: ~A"
+	       :format-arguments (list (unix:get-unix-error-msg))
+	       :errno (unix:unix-errno)))      
+      ;; Added by Chun Tian (binghe)
+      (when (and local-host local-port)
+	(bind-inet-socket socket local-host local-port))
+      socket)))
+
+) ; ext:without-package-locks
+
 (in-package :usocket)
 
 #+win32
@@ -50,13 +120,16 @@
                                                :socket socket
                                                :condition condition))))
 
-(defun socket-connect (host port &key (element-type 'character) timeout)
+(defun socket-connect (host port &key (element-type 'character) timeout
+		                      local-host local-port)
   (when timeout
     (warn "SOCKET-CONNECT timeout not supported in CMUCL"))
   (let* ((socket))
     (setf socket
           (with-mapped-conditions (socket)
-             (ext:connect-to-inet-socket (host-to-hbo host) port :stream)))
+             (ext:connect-to-inet-socket (host-to-hbo host) port :stream
+					 :local-host (host-to-hbo local-host)
+					 :local-port local-port)))
     (if socket
         (let* ((stream (sys:make-fd-stream socket :input t :output t
                                            :element-type element-type
Index: backend/sbcl.lisp
===================================================================
--- backend/sbcl.lisp	(revision 362)
+++ backend/sbcl.lisp	(working copy)
@@ -184,23 +184,30 @@
                      (signal usock-cond :socket socket))))))
 
 
-(defun socket-connect (host port &key (element-type 'character) timeout deadline)
+(defun socket-connect (host port &key (element-type 'character) timeout deadline
+		                      local-host local-port)
   (declare (ignore deadline))
   (when timeout
     (warn "SOCKET-CONNECT timeout not supported in SBCL"))
-  (let* ((socket (make-instance 'sb-bsd-sockets:inet-socket
-                                :type :stream :protocol :tcp))
-         (stream (sb-bsd-sockets:socket-make-stream socket
-                                                    :input t
-                                                    :output t
-                                                    :buffering :full
-                                                    :element-type element-type))
-         ;;###FIXME: The above line probably needs an :external-format
-         (usocket (make-stream-socket :stream stream :socket socket))
-         (ip (host-to-vector-quad host)))
-    (with-mapped-conditions (usocket)
-      (sb-bsd-sockets:socket-connect socket ip port))
-    usocket))
+  (let ((socket (make-instance 'sb-bsd-sockets:inet-socket
+                                :type :stream :protocol :tcp)))
+    (when (and local-host local-port)
+      (let ((local-address (if (stringp local-host)
+			       (sb-bsd-sockets:host-ent-address
+				(sb-bsd-sockets:get-host-by-name local-host))
+			       local-host)))
+	(sb-bsd-sockets:socket-bind socket local-address local-port)))
+    (let* ((stream (sb-bsd-sockets:socket-make-stream socket
+						      :input t
+						      :output t
+						      :buffering :full
+						      :element-type element-type))
+	   ;;###FIXME: The above line probably needs an :external-format
+	   (usocket (make-stream-socket :stream stream :socket socket))
+	   (ip (host-to-vector-quad host)))
+      (with-mapped-conditions (usocket)
+	(sb-bsd-sockets:socket-connect socket ip port))
+      usocket)))
 
 (defun socket-listen (host port
                            &key reuseaddress
Index: backend/allegro.lisp
===================================================================
--- backend/allegro.lisp	(revision 362)
+++ backend/allegro.lisp	(working copy)
@@ -49,7 +49,8 @@
       :text
     :binary))
 
-(defun socket-connect (host port &key (element-type 'character) timeout)
+(defun socket-connect (host port &key (element-type 'character) timeout
+                                      local-host local-port)
   (let ((socket))
     (setf socket
           (with-mapped-conditions (socket)
@@ -57,9 +58,13 @@
                 (mp:with-timeout (timeout nil)
                   (socket:make-socket :remote-host (host-to-hostname host)
                                       :remote-port port
+				      :local-host local-host
+				      :local-port local-port
                                       :format (to-format element-type)))
                 (socket:make-socket :remote-host (host-to-hostname host)
                                     :remote-port port
+				    :local-host local-host
+				    :local-port local-port
                                     :format (to-format element-type)))))
     (make-stream-socket :socket socket :stream socket)))
 
