Process the entire socket buffer more frequently.
Invert conditions to avoid possible deadlocks.
Finalize politely.
Change login position.
Decrease recv size.
Close sockets separately.