For Enhanced message, add auto close connection when idle to avoid long-term connection error.

* Add auto close connection when idle to avoid long-term connection error. Since if connection created and idle for more than 2 hours, the actual connection might broken but python runtime doesn't get informed, it will enter weird state
* Add timeout and retry for writing data to socket connection
* Refactor: refine log and wrap worker thread as class to improve readability