Fixed #1085
WARNING: In this commit was modified and poorly tested closest port selection system.
Merge pull request #1135 from dvvrd/fix1085

Fixed #1085