Rewrite networking code for the 100th time to fix memory leak