# TWS is running but not accepting the connection

> The banner says TWS or IB Gateway is running but not accepting the connection: enable the API socket, allow Scalpel or trust 127.0.0.1, and reconnect.

Source: https://scalpeltrade.com/guides/fix-not-accepting-connection
Guide 13.2, Troubleshooting. Beginner, 4 min read.
Checked with Scalpel 1.12.8 on 2026-10-15. By Scalpel Team.

**In short:**

The banner means TWS or IB Gateway is up on the port Scalpel uses, but its API is refusing the connection. In TWS open **Configure** > **API** > **Settings** (in IB Gateway, **Configure** > **Settings** > **API**), tick **Enable ActiveX and Socket Clients**, add **127.0.0.1** to **Trusted IPs**, click **OK**, and accept the connection if TWS asks. Scalpel retries by itself and connects within seconds.

Scalpel found the program (the port answered) but the handshake that follows never completed. That is almost always a setting or a question waiting inside TWS, not a network problem.

## TWS is running on port 7497 but is not accepting the connection. In Configure > API > Settings, enable the API socket and allow Scalpel when it asks.

With IB Gateway the banner reads "IB Gateway is running on port 4001 but is not accepting the connection. In Configure > Settings > API, enable the API socket and allow Scalpel when it asks." The status bar pill keeps reading **Disconnected** while Scalpel retries.

*Screenshot: The program answered on the port, but refused the API connection. Demo data.*

## Fix it

### 1. Look for a waiting question in TWS

Switch to TWS or IB Gateway and look for a small window asking whether to accept an incoming API connection. It can open behind other windows. Accept it. Scalpel opens several connections (quotes, depth, orders, scanner), so it may ask more than once.

### 2. Enable the API socket

Open the API settings: in TWS, **Configure** (the gear, or **File** > **Global Configuration**) > **API** > **Settings**; in IB Gateway, **Configure** > **Settings** > **API** > **Settings**. Tick **Enable ActiveX and Socket Clients** (1).

*Screenshot: Simplified TWS API settings: Enable ActiveX and Socket Clients (1), Read-Only API off (2), the socket port (3), and 127.0.0.1 as a trusted IP (4). Not the real TWS window.*

### 3. Trust this computer

Under **Trusted IPs**, click **Create** and add **127.0.0.1** (4). TWS then stops asking every time Scalpel connects. Leave **Allow connections from localhost only** ticked: Scalpel runs on the same computer.

### 4. Untick Read-Only API

**Read-Only API** (2) does not stop the connection, but it makes TWS refuse every order Scalpel sends in LIVE. Untick it now so you do not meet it mid-trade ([guide 13.5](https://scalpeltrade.com/guides/fix-order-rejected)).

### 5. Click OK and wait a few seconds

Click **OK** (or **Apply** and **OK**). Scalpel retries by itself; the pill turns green and reads **Connected**. No restart of Scalpel is needed.

## Still not accepting?

- **Another copy of Scalpel is open,** or another program uses Scalpel's API client ids. Close it. The status bar may read "Disconnected: TWS refused the connection (client id in use?), retrying".
- **IB Gateway or TWS has just logged in** and is still loading. Give it 30 seconds.
- **A dialog is open in TWS**, such as a message after login. Close it; TWS can hold API connections until it is dismissed.
- **The port belongs to something else.** The banner checks only that the port answers. If you changed TWS's port, make sure no other program uses the same number.
- **Restart TWS or IB Gateway** after changing API settings if nothing else works. Scalpel reconnects by itself when it comes back.

For the full TWS and IB Gateway setup, see [guide 1.5](https://scalpeltrade.com/guides/enable-tws-api). For other banner messages, see [guide 13.1](https://scalpeltrade.com/guides/fix-disconnected).

## Questions

**Where is Enable ActiveX and Socket Clients?**

In TWS: Configure (or File > Global Configuration) > API > Settings. In IB Gateway: Configure > Settings > API > Settings.

**Why does TWS keep asking me to accept the connection?**

Because 127.0.0.1 is not a trusted IP. Add it under Trusted IPs in the API settings and TWS stops asking.

**Should Read-Only API be on or off?**

Off, if you want to place LIVE orders from Scalpel. With it on, Scalpel connects and shows data but every order is refused.

**Do I need to restart Scalpel after changing TWS settings?**

No. Scalpel retries the connection on its own every few seconds.
