This is to help stimulate some discussion on the general problem of binding sockets for outgoing connections. But I'm also posting a temporary answer to jknair's need.
jknair's desire was to bind an outgoing HTTP connection to a particular source address. This is reasonable and necessary at times when a machine is multi-homed, having more than one interface and/or network address. For instance, sometimes firewall rules can be quite picky about source/destination address pairs. If you aren't coming from the correct interface your traffic will be rejected.
node currently only handles binding _server_ addresses. A server must be bound to a particular address(es) in order to listen for incoming connections from that address(es). That there are dropout rules which allow one to just say "any address on this machine" does not change that a bind must be done, it just may not be obvious what addresses those are.
Maarten and I had a need for binding other outgoing sockets besides HTTP client requests.
(see
https://groups.google.com/d/topic/nodejs/PwY1uiPUSoM/discussion)
Again, there is no provision by node for generally binding sockets. The net.js/http.js code has not been 'shaped' by the need, yet.
But Maarten threw out a wild idea (hack) for getting around the difficulty that binding is only available for servers. He suggested using part of the internal API for server, looking inside net.Server.listen to find the call to net._createServerHandle() that does the actual calls to bind()/bind6().
I've used that idea plus pawing through http.js to come up with a temporary answer for jknair. I hope it is forgivable.
Please check gist "Example of binding socket for nodejs HTTP client request (for discussion only)"
https://gist.github.com/1412297I noticed that http.js Agent() had a hook for socket creation, self.createConnection. (Thank you @bnoordhuis) Together with Maarten's idea I created a derived Agent that uses its own createConnection() to force binding of new client sockets. This is found in lines 39-60 of the gist, and then enabled in line 69. See the third part of the gist for output showing the bind working on the client socket.
This idea would 'work' for jknair, at least until something changes within net.js to disable Maarten's workaround. But the acrobatics required are rather unsatisfying.... Any reasons not to work towards a better implementation for binding outgoing sockets?