Factorize socket setup code for socket opening

This patch is factorizing the socket opening code between IPv4 and IPv6.
Socket option are the same for IPv4 and IPv6, so we can factorize the code.
As a side effect this patch brings output device selection to IPv4.