Factor out I/O Port processing.

Get rid of some repeated code that would need changing in two places if ports
were to change.