Lots of changes to sockets. First working version.