several bug fixes

1. fix non-ascii characters length. Useful if the terminal output contains fancy symbols or non English.
2. fix client-socket close. If one client exits, the server socket will not crash. This enables multiple terminal tabs under socket mode.
3. remove trailing space. Please pardon my revise, I hope this revise follows your coding style