fixes #6599 improve getAvailableESPort logic