Added support for when the hostname resolves to multiple IP addresses. Also cleaned up a bit.