modified code for ports handling