# Session 08: Client Connection

## Summary

The client connection handler — manages individual client TCP connections, message processing, subscription management, and client lifecycle. The largest single class in the server.

## Scope

| Go File | Features | Feature IDs | Go LOC |
|---------|----------|-------------|--------|
| server/client.go | 185 | 387–571 | 5,680 |
| server/client_proxyproto.go | 10 | 572–581 | 273 |
| **Total** | **195** | | **5,953** |

## .NET Classes

- `ClientConnection` — client state, read/write loops, publish, subscribe, unsubscribe
- `ClientFlag` — client state flags
- `ClientInfo` — client metadata
- `ProxyProtocolAddress` — PROXY protocol v1/v2 parsing

## Test Files

| Test File | Tests | Test IDs |
|-----------|-------|----------|
| server/client_test.go | 82 | 182–263 |
| server/client_proxyproto_test.go | 23 | 159–181 |
| server/closed_conns_test.go | 7 | 264–270 |
| server/ping_test.go | 1 | 2615 |
| **Total** | **113** | |

## Dependencies

- Session 01 (Foundation Types)
- Session 02 (Utilities — queues)
- Session 03 (Configuration — ServerOptions)
- Session 05 (Subscription Index)
- Session 07 (Protocol Parser)

## .NET Target Location

- `dotnet/src/ZB.MOM.NatsNet.Server/ClientConnection.cs`

## Notes

- This is the core networking class — every connected client has one
- Heavy use of `sync.Mutex` in Go → consider `lock` or `SemaphoreSlim`
- Write coalescing and flush logic is performance-critical
- May need partial class split: `ClientConnection.Read.cs`, `ClientConnection.Write.cs`, etc.
