Not sure but the doco says that getaddrinfo returns 0 for success. You should change your test to != 0 rather than < 0. It may be that all the error codes it can return are < 0 but it's still incorrect.
Also, res will be set to point to the head of a linked list of results. It may be that the first item in that list is not ai_family == AF_INET and some of the members you're accessing are not valid for the family of that item.
You could initialise res to NULL and then test for res != NULL to make sure getaddrinfo is actually populating the pointer.
from
http://linux.die.net/man/3/getaddrinfo[
^]
getaddrinfo(3) sets res to point to a dynamically-allocated linked list of addrinfo structures, linked by the ai_next member. There are several reasons why the linked list may have more than one addrinfo structure, including: if the network host is multi-homed; or if the same service is available from multiple socket protocols (one SOCK_STREAM address and another SOCK_DGRAM address, for example).