public void authenticate() throws ParseException, IOException {

            String string;
            Boolean exit = false;
            JSONObject json;
            
            // If not authenticated already, commence
            if (!authenticated) {

                try {
                    // Keep reading output from server
                    while ((string = bufferedreader.readLine()) != null) {
                        
                        // Log server output
                        ActivationServiceClient.log(string);
                        
                        // Parse JSON from string
                        json = (JSONObject)new JSONParser().parse(string);
                        JSONObject reply = new JSONObject();
                        String message = (String) json.get("message");

                        // Server asking for authentication
                        if (message.equals("authenticate")) {
                            ActivationServiceClient.log("System asking for"
                                    + " authorization.");
                            // Send username/password to server
                            reply.put("message", "readKey");
                            reply.put("username", username);
                            reply.put("password", password);
                            bufferedwriter.write(reply.toJSONString() + "\r\n");
                            bufferedwriter.flush();
                        } // Authentication failed
                        else if (message.equals("authenticationFailed")) {
                            // Show the login panel again
                            loginPanel("Please Login to continue.");
                            ActivationServiceClient.log("Authentication Failed.");
                            // Notify user using Pop Up
                            JOptionPane.showMessageDialog(ActivationServiceClient.this,
                                "Authentication Failed. Try again.",
                                "Error Message",
                                JOptionPane.ERROR_MESSAGE);
                            return;
                        } // Authentication with server successful
                        else if (message.equals("authenticated")) {
                            ActivationServiceClient.log("Authenticated.");
                            authenticated = true;
                            // Show Options after login
                            optionPanel();
                            // Notify user using Pop Up
                            JOptionPane.showMessageDialog(ActivationServiceClient.this,
                                "Successfully logged in.");
                            return;
                        } // Server behaved unexpectedly, re-login
                        else {
                            // Show Login Panel
                            loginPanel("Please Login to continue.");
                            // Notify user using Pop Up
                            JOptionPane.showMessageDialog(ActivationServiceClient.this,
                                "Server responded in an unexpected way. Please"
                                    + " restart the application and try again.",
                                "Error Message",
                                JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    }

                }
                catch (SocketException se) {
                    ActivationServiceClient.log("Socket Exception, re-establishing"
                            + " connection.");
                    reconnect();
                    return;
                }
                catch (SSLHandshakeException ssl) {
                    ActivationServiceClient.log("SSL Exception, re-establishing"
                            + " connection");
                    reconnect();
                    return;
                }
                catch (IOException e) {
                    ActivationServiceClient.log("IO Exception, re-establishing"
                            + " connection.");
                    reconnect();
                    return;
                }
                catch (Exception e) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    e.printStackTrace(new PrintStream(out));
                    ActivationServiceClient.log(new String(out.toByteArray()));
                    reconnect();
                    return;
                }
            }
            
            // If already authenticated, show the Login screen and
            // notify using Pop Ups
            optionPanel();
            JOptionPane.showMessageDialog(ActivationServiceClient.this,
                "You are already logged in.");
            return;
        }