Fix a memory trample and some bad freeaddrinfo() calls.

The freeaddrinfo() commits are a replacement for #1918.
Line 205, at the first ```getaddrinfo()``` call (github does not allow me to comment on that line), I can see we don't free anything. Maybe you want to apply the same logic and amend to this pull request, or you want me to create a new one?