refactor reading and writing into Socket, recognize upgrade