Improved port detection