Use getaddinfo instead of gethostbyname.