fixed memory leaks
improved polling logic for socket