remove race condition from tests from the "language level"

Using channels instead of variables (and a bit of refactoring) removes race conditions from the "language level". There's still a race condition on the level of TCP connections.

It seems to me that this code is better in a sense that communication between goroutines doesn't involve reading and writing from the same variable concurrently.