Added EOF check to not close port prematurely