refactoring to avoid connecting to a port if found