---
title: "Fix Phone Gateway errors in SayHi: 4401, 4406, 4409 and HTTP 503"
description: "What each close code, HTTP status and reject reason means when your phone gateway talks to SayHi, and what the gateway should do next."
url: https://sapps.co/explore/sayhi/articles/fix-phone-gateway-connection-errors
app: "SayHi"
app_url: https://sapps.co/explore/sayhi
published: 2026-12-02T07:33:34.415Z
updated: 2026-12-02T09:42:24.598Z
tags: ["phone gateway","telephony","troubleshooting","websocket"]
lang: "en"
---

# Fix Phone Gateway errors in SayHi: 4401, 4406, 4409 and HTTP 503

When SayHi refuses or ends a phone gateway connection, it says why with a close code (`4401`, `4406`, `4408`, `4409`), an `error` message, a `reject_incoming` reason or a `hangup`. Find what your gateway saw in the table, then follow its fix. If you haven't connected a gateway yet, start with [How to connect your own phone gateway to SayHi](https://sapps.co/explore/sayhi/articles/connect-phone-gateway-websocket).

## Find your symptom

| What the gateway sees | Why | What to do |
| --- | --- | --- |
| Handshake succeeds, then close `4401` | Token missing, wrong, regenerated or revoked, or the configuration is inactive | Stop retrying; paste a new token |
| HTTP 503 at the control handshake | SayHi is down, or a proxy refused the token | Retry with backoff |
| Handshake refused, `Authorization` header sent | The platform router refuses that header | Send `X-Gateway-Token` |
| `error protocol_unsupported`, then `4406` | `hello` did not say `"protocol":1` | Update the client; don't reconnect |
| `error audio_unsupported`, then `4406` | No 8000 Hz `pcm16` or `ulaw` offered | Fix the `audio` block |
| `bad_message`, then `4406` | No `hello` within 10 s, or another message came first | Send `hello` first |
| Close `4409` on the control socket | Another connection with the same token sent `hello` | Don't reconnect from this one |
| Close `4409` on a media socket | The media URL was opened again after the call started | No resume: the call is over |
| Close `4408` | Nothing received for 45 s | Reconnect, and ping every 15 s |
| `welcome` line with `accepted:false` | `unknown_line` or `other_device` | Add the line ID in SayHi (`unknown_line`) or assign the line to this device (`other_device`) |
| `reject_incoming` | `no_workflow`, `busy`, `quota` or `unknown_line` | See below |
| `hangup` for a run you were dialing | No `dial_ack` arrived within 5 s, or it arrived later | Hang up that phone call |

## Close 4401: token rejected

SayHi accepts every control handshake first and checks the token afterwards. A refused gateway therefore gets close code `4401`, instead of an HTTP error status, which a proxy in front of SayHi may rewrite. A `4401` arrives when:

- the `X-Gateway-Token` header is missing, malformed or unknown;
- the Phone Gateway configuration is inactive;
- someone pressed **Regenerate** or **Revoke** on the device, or deleted it (a live socket is closed with `4401` at once);
- the device was revoked while an incoming call was being set up.

Treat `4401` as final. The reference client (`port/tools/phone-gateway-client.py` in the SayHi source archive, behind the **Source code** link) prints `Token rejected` and stops. Retrying unchanged can't succeed. If the header is missing, send it. If the configuration is inactive, make it active again (the token still works).

Otherwise press **Regenerate**, copy the new value from **Device token (X-Gateway-Token header)** (SayHi shows it only once and keeps just a hash), paste it into the gateway and reconnect. A revoked device shows **Revoked** in its status.

## HTTP 503 at the handshake

A 503 before the socket opens is ambiguous: SayHi may be unavailable, or a proxy in front of it may have rejected the token. The gateway can't tell which, so the protocol says to keep retrying with backoff and to show the user both possibilities. The reference client waits 1, 2, 5, 10 and then 30 seconds between attempts and logs:

```
HTTP 503: SayHi is unavailable, or the token was rejected by a proxy
```

If 503s continue for a long time, check that the SayHi server is running before replacing the token.

## The token is in the wrong header

The token goes only in `X-Gateway-Token`. Never put it in `Authorization` (the platform router refuses that header), in the URL or in a message:

```
X-Gateway-Token: pgw1.…
```

Browsers can't set custom headers on a WebSocket, so browser softphones can't connect with protocol 1. No `Origin` header is needed.

## Close 4406: protocol, audio or hello

The server sends an `error` message first, then closes with `4406`. The message tells you which problem it is:

```
{"type":"error","code":"protocol_unsupported","message":"this server speaks Phone Gateway protocol 1"}
{"type":"error","code":"audio_unsupported","message":"this server needs 8000 Hz audio in pcm16 or ulaw"}
{"type":"error","code":"bad_message","message":"hello was not received in time"}
{"type":"error","code":"bad_message","message":"the first message must be hello"}
```

For audio, list `pcm16`, `ulaw` or both in `audio.formats`. If you send `sample_rates`, include `8000`; a client that lists no rates is taken to mean 8 kHz. The `hello` must be the first message and must arrive within 10 seconds of connecting. Control messages are JSON text frames; a binary frame gets a `bad_message` error.

## Close 4409: replaced

On the control socket, `4409` means a newer connection sent `hello` with the same device token. SayHi sends commands only to the newest socket. This happens when two copies of the gateway run with one token, or when a gateway reconnects without closing its old socket. Don't reconnect from a socket closed with `4409`: if both copies reconnect, each new `hello` closes the other. Give each physical gateway its own device and token.

On a media socket, `4409` means the media URL was opened again after the run had started. The URL can be presented more than once, but only the first connection is used. There is no resume: when a media socket closes, the call ends, so the gateway should hang up the phone call and send `call_state ended`.

## Close 4408 and "offline after 45 s"

A device is offline after 45 seconds with nothing received, and its socket is closed with `4408`. Send JSON `{"type":"ping"}` every 15 seconds and answer the server's `ping` with `{"type":"pong"}`. WebSocket ping frames don't count.

When a device goes offline, its calls without a media socket (still ringing) fail with the reason `The phone gateway went offline`, and their lines are freed. Calls with a live media socket end through the media socket. Reconnect after `4408`, and list the calls the phone is still carrying in `hello.active_calls`. SayHi sends `hangup` for each listed call that is no longer live, and fails live calls you didn't list with `The phone gateway reconnected without this call`.

For any close code other than `4401`, `4406` and `4409`, the reference client reconnects with the same backoff.

## Lines not accepted in welcome

`welcome` reports each line the gateway announced:

```json
{"line_id":"sim2","accepted":false,"reason":"unknown_line"}
```

- `unknown_line`: SayHi has no line with that line ID. Add it in SayHi with the exact ID the gateway announces.
- `other_device`: the line exists but is assigned to another device. Assign it to this device, or announce it only from the device it belongs to.

The line configuration in SayHi wins: the `number` in `hello` is informational. A line you add in SayHi for an ID the gateway already announced works without reconnecting.

## Incoming calls rejected

After `incoming_call`, SayHi replies with `media_url` or `reject_incoming`. The reasons are:

- `unknown_line`: the line wasn't announced in this connection's `hello`, doesn't exist in SayHi, or belongs to another device.
- `no_workflow`: the line has no **Inbound agent**, or that agent was not found.
- `busy`: the line or the device is at its calls-at-once limit, the organization is at its concurrent call limit, or SayHi hit an internal error while setting up the call.
- `quota`: the organization's quota does not allow another call.

If no reply arrives within 8 seconds, the gateway rejects the phone call and sends `{"type":"call_state","call_id":"…","state":"failed","reason":"timeout"}`; SayHi then fails any run it created and frees the line. After a `media_url`, open the media socket within 30 seconds; otherwise the run fails with `The phone gateway did not open the media socket`.

## Outbound calls fail

For an outbound call SayHi sends `dial` and waits 5 seconds for `dial_ack` or `dial_reject`. The run fails with one of these reasons (`<reason>` is what your `dial_reject` sent):

```
The phone gateway did not acknowledge the dial within 5 seconds
The phone gateway rejected the dial: <reason>
No phone gateway line is online
Every phone gateway line is busy
Every phone gateway device is at its call limit
The phone gateway is not connected
The Phone Gateway configuration is inactive
This run has no Phone Gateway configuration
```

When the 5 seconds pass, SayHi also sends `hangup` for that run, so stop dialing. A `dial_ack` that arrives after the 5 seconds gets a `hangup` carrying the same `run_id` and `call_id`; hang up that phone call. `dial_reject` accepts only `busy`, `no_line`, `not_ready` and `unsupported`; any other reason gets a `bad_message` error.

## Media socket refused at the handshake

The media URL carries a token signed for one workflow, organization and run. If it is missing, altered or expired, SayHi refuses the upgrade with HTTP 403 before the call pipeline starts. In SayHi 1.12.0's release checks, the same three refusals (missing, forged and expired token) came back as HTTP 503 through the public endpoint. Don't retry a media URL that was refused. Use the URL from `media_url` exactly as received, and keep it private.

To test these fixes against a SayHi server of your own, [set one up from the SayHi Store page](https://sapps.co/explore/sayhi).
