Fix race condition in socket sending which could result in infinite hang