Move common socket routine into separate function

Signed-off-by: Max <max.suraev@fairwaves.co>