fixed memory leak in socket function