Fixed issue with ungraceful termination, implemented a keep alive timer and handled other network connection events