Just so people know, connect()ing a UDP socket and calling getsockname() to get the local interface doesn't work on Solaris 2.0 through 2.5.1 (it works in 2.6) and I've heard it also doesn't work in winsock. You always get INADDR_ANY back. So it's good to have fallback behavior of some kind for broken systems.