private void configure(final JDialog configDialog, final int portNumber) {
        try {
            Utils.logToConsole("Performing port configuration");
            
            if (!Utils.selectConfigSection(configDialog, new String[] {"API","Settings"}))
                // older versions of TWS don't have the Settings node below the API node
                Utils.selectConfigSection(configDialog, new String[] {"API"});

            Component comp = SwingUtils.findComponent(configDialog, "Socket port");
            if (comp == null) throw new IBControllerException("could not find socket port component");

            JTextField tf = SwingUtils.findTextField((Container)comp, 0);
            if (tf == null) throw new IBControllerException("could not find socket port field");
            
            int currentPort = Integer.parseInt(tf.getText());
            if (currentPort == portNumber) {
                Utils.logToConsole("TWS API socket port is already set to " + tf.getText());
            } else {
                if (!isGateway) {
                    JCheckBox cb = SwingUtils.findCheckBox(configDialog, "Enable ActiveX and Socket Clients");
                    if (cb == null) throw new IBControllerException("could not find Enable ActiveX checkbox");
                    if (cb.isSelected()) ConfigDialogManager.configDialogManager().setApiConfigChangeConfirmationExpected(true);
                }
                Utils.logToConsole("TWS API socket port was set to " + tf.getText());
                tf.setText(Integer.toString(portNumber));
                Utils.logToConsole("TWS API socket port now set to " + tf.getText());
            }

            SwingUtils.clickButton(configDialog, "OK");

            configDialog.setVisible(false);
        } catch (IBControllerException e) {
            Utils.logError(e.getMessage());
        }
    }