Fixed the issues commented in the pull request
Avoid unuseful lookups when hostname not valid