Detect libsocket and libnsl automatically

Automatically detect if libsocket and libnsl are needed.