added termination code; removed spin code
fixing sleep lock bug when the app goes to sleep
merged two code, now having bug of connecting