class DlmsConnectionHelper {

    public DlmsConnectionManager createConnectionForDevice(final DlmsDevice device,
            final DlmsMessageListener messageListener) throws OsgpException {
        if (device.needsInvocationCounter() && !device.isInvocationCounterInitialized()) {
            this.invocationCounterManager.initializeInvocationCounter(device);
        }

        try {
            return this.connectionFactory.getConnection(device, messageListener);
        } catch (final ConnectionException e) {
            if (device.needsInvocationCounter() && this.indicatesInvocationCounterOutOfSync(e)) {
                this.resetInvocationCounter(device);
            }
            // Retrow exception, for two reasons:
            // - The error should still be logged, since it can be caused by a problem other than the invocation
            //   counter being out of sync.
            // - This will cause a retry header to be set so the operation will be retried.
            throw e;
        }
    }

    @Autowired public  DlmsConnectionHelper(final InvocationCounterManager invocationCounterManager,
            final DlmsConnectionFactory connectionFactory);

}