You didn't give me enough code to tell if this is will fix it but
here it goes:
If you are extending Frame, then go to the method where you are
checking for the Window close, when it closes, just disconnect. If
you're extending JFrame, you'll have to overwrite their method.
Sorry I would elaborate but the bell rings in 2 minutes and then I
have US History.