Fixed #1085

WARNING: In this request was modified and poorly tested closest port selection system.