onDisconnect instead feof

feof() throws error if socket is null: 

Warning: feof(): 32 is not a valid stream resource in - on line 587