Fixed issues that made an incorrect port being used under certain conditions (issue 217)