Changed the direction determination code to avoid repetitive probing of ports.