You’d be better off asking this in a JAVA forum, but I can tell you where this goes wrong:
line.toLowerCase().startsWith("PING ")
You’re comparing a lower case String to an upper case one.
Also, to make it easier on yourself, you can instead of writing and then flushing to the socket, use the println() method on a PrintWriter to write to the output stream (this will then send a new line without all the extra code)