Fixed bug that could cause connection to close prematurely.

Fixed bug that could cause connection to close prematurely.
Fixed an segmentation fault in case of exiting XBMC.