code Refactoring

Refactored code
fixed localAddress problem
decreased connection timeout
changed handler to avoid memory leakage