Add socket ping code, and handle a few more error cases.