Fix infinite loop when tcp socket has been disconnected

Signed-off-by: Alexander Lukichev <alexander.lukichev@espotel.com>