Factor out socket binding code for IPv4 and IPv6.