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

The freeaddrinfo() commits are a replacement for #1918.
Thanks for pointing that out - one more commit pushed just now.