working version with socket stack