Fix socket race condition which can cause bytes to be dropped

Rather than discarding data if it is not a complete packet, keep
appending until a complete packet is formed.