Refactor NIO server and optimize