Fix ports array out of bounds issue