If unit test need some external device that have some unknown state then we don't have unit tests, but maybe system tests.
Unit tests shall execute as fast as possible, without any external device and possible before each commit.
Proposal: mock sockets, and classes to have control over states.