aboutsummaryrefslogblamecommitdiffstats
path: root/lib/megaco/src/engine/megaco_messenger.erl
blob: 1d462b2140f3ae7ccad04fdd29afeda01ff63859 (plain) (tree)
1
2
3
4
5
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350

                   
  
                                                        
  










                                                                           
  




















































































                                                                               















                                                                                  

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                              
                                           

















                                                              


                                                               


                                                                
                                                      





                                                                      


                                                       















                                                                             





                                                              
                                        
                                               














































































































                                                                               





                                                              
                               
                                            
















                                                                            






                                                              






































                                                                             
                                           



                                                                      
                                                                     

                                                        
                             





















































































                                                                              






                                                                   


                                                                             

                                                                
                                                                           

                                                    
                                                                        





                                                                   
                                                 




                                                  











                                                                             


        





















































                                                                            

                                                   











                                                                   

                                           















                                                                          

                                                                     





































                                                                       

                                                                     








































                                                                        

                                           






                                                                   

                                           










































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                         



                                                               
                                        
                                                              

                                               





















                                                                      
                                         


















                                                                   


                                                                 
                                                                

                                                       
                                                                 




                                                               




                                                 

                                                                          








                                                                









                                                                               








                                                           





























                                                                    
                                                 




























































































































                                                                            



                                                           






















                                                                           


                                                                



































                                                                              

                                  































































































































                                                                                    
                                                                             










                                                                              







                                                                     









































































































































































































































































































































































































































































































































































































































































































                                                                                         




                                                                    







































































































































































































































































































































































































































































































































































































                                                                               
                                                 
























































                                                                                    


                                                                         
                                                        































                                                                               

                                                                  
                                                     


                                                                              


















                                                                  


                                                                     


                                                                   


                                                         






















































































































































































                                                                                       
                                            


                                    


                                                       


                                                               













                                                                           







                                                                          


                                                               
                                                               














                                                                     









                                                                    

                                                  

                                                              

                                          



























































































                                                                             


                                                  
                                                          
                               
                                                                   



















                                                                              



                                                            




                                                                       




































































































                                                                                  



                                                                       














































































                                                                                       

                                            



                                                  

                                          












































































































































































































                                                                             






















































                                                                           
                                                                   


                          

































                                                                       
                             























                                                                   
 
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%%
%%----------------------------------------------------------------------
%% Purpose: Send and process a (sequence of) Megaco/H.248 transactions
%%----------------------------------------------------------------------

-module(megaco_messenger).

%% Application internal export
-export([
         process_received_message/4, process_received_message/5,
         receive_message/4, receive_message/5,
         connect/4, connect/5, 
         disconnect/2,
         encode_actions/3, 
         call/3,
         cast/3,
         cancel/2,
         request_timeout/2,
	 request_keep_alive_timeout/2, 
         pending_timeout/3,
         reply_timeout/3,
         segment_timeout/3,
         %% segment_reply_timeout/4,

	 test_request/5,
	 test_reply/5
        ]).

%% MIB stat functions
-export([
	 get_stats/0, get_stats/1, get_stats/2,
	 reset_stats/0, reset_stats/1
	]).

%% Misc functions
-export([
	 cleanup/2, 
	 which_requests/1, which_replies/1
	]).

%% Module internal export
-export([
	 process_received_message/6,
         handle_request/2,
         handle_long_request/2,
         connect_remote/3,
         disconnect_local/2,
         disconnect_remote/3,
         send_request_remote/4,
         receive_reply_remote/2, receive_reply_remote/3
        ]).

-include_lib("megaco/include/megaco.hrl").
-include("megaco_message_internal.hrl").
-include_lib("megaco/src/app/megaco_internal.hrl").

%% N.B. Update cancel/1 with '_' when a new field is added
-record(request,
        {trans_id,
	 remote_mid,
         timer_ref,  % {short, Ref} | {long, Ref}
         init_timer,
         init_long_timer,
         curr_timer,
         version,
         bytes,     % {send, Data} | {no_send, Data}, Data = binary() | tuple()
         send_handle,
         user_mod,
         user_args,
         reply_action, % call | cast
         reply_data,
	 seg_recv = [],     % [integer()] (received segments)
	 init_seg_timer,
	 seg_timer_ref,
	 keep_alive_timer,  % plain | integer() >= 0
	 keep_alive_ref     % undefined | ref()
        }).


%% N.B. Update cancel/1 with '_' when a new field is added
-record(reply,
        {
	 trans_id,
	 local_mid,
	 state = prepare,     % prepare | eval_request | waiting_for_ack | aborted
	 pending_timer_ref,
	 handler = undefined, % pid of the proc executing the callback func
	 timer_ref,
	 version,
	 %% bytes: Sent reply data: not acknowledged
	 bytes,               % binary() | [{integer(), binary(), timer_ref()}]
	 ack_action,          % discard_ack | {handle_ack, Data}
	 send_handle,
	 %% segments: Not sent reply data (segments)
	 segments = [],       % [{integer(), binary()}]
	 user_mod,
	 user_args
	}).

-record(trans_id,
        {
          mid,
          serial
         }).


-ifdef(MEGACO_TEST_CODE).
-define(SIM(Other,Where),
	fun(Afun,Bfun) ->
		Kfun = {?MODULE,Bfun},
		case (catch ets:lookup(megaco_test_data, Kfun)) of
		    [{Kfun,Cfun}] ->
			Cfun(Afun);
		    _ ->
			Afun
		end
	end(Other,Where)).
-define(TC_AWAIT_CANCEL_EVENT(),
	case megaco_tc_controller:lookup(block_on_cancel) of
	    {value, {Tag, Pid}} when is_pid(Pid) ->
		Pid ! {Tag, self()},
		receive
		    {Tag, Pid} ->
			ok
		end;
	    {value, {sleep, To}} when is_integer(To) andalso (To > 0) ->
		receive after To -> ok end;
	    _ ->
		ok
	end).
-define(TC_AWAIT_REPLY_EVENT(Info),
	case megaco_tc_controller:lookup(block_on_reply) of
	    {value, {Tag, Pid}} when is_pid(Pid) ->
		Pid ! {Tag, self(), Info},
		receive
		    {Tag, Pid} ->
			ok
		end;
	    _Whatever ->
		%% io:format("Whatever: ~p~n", [Whatever]),
		ok
	end).
-else.
-define(SIM(Other,Where),Other).
-define(TC_AWAIT_CANCEL_EVENT(),ok).
-define(TC_AWAIT_REPLY_EVENT(_),ok).
-endif.


-define(report_pending_limit_exceeded(ConnData),
	?report_important(ConnData, "<ERROR> pending limit exceeded", [])).

-ifdef(megaco_extended_trace).
-define(rt1(T,F,A),?report_trace(T,F,A)).
-define(rt2(F,A),  ?rt1(ignore,F,A)).
-define(rt3(F),    ?rt2(F,[])).
-else.
-define(rt1(T,F,A),ok).
-define(rt2(F,A),  ok).
-define(rt3(F),    ok).
-endif.


%%----------------------------------------------------------------------
%% SNMP statistics handling functions
%%----------------------------------------------------------------------

%%-----------------------------------------------------------------
%% Func: get_stats/0, get_stats/1, get_stats/2
%% Description: Retreive statistics (counters) for TCP
%%-----------------------------------------------------------------

get_stats() ->
    megaco_stats:get_stats(megaco_stats).

get_stats(ConnHandleOrCounter) ->
    megaco_stats:get_stats(megaco_stats, ConnHandleOrCounter).

get_stats(ConnHandle, Counter) ->
    megaco_stats:get_stats(megaco_stats, ConnHandle, Counter).


%%-----------------------------------------------------------------
%% Func: reset_stats/0, reaet_stats/1
%% Description: Reset statistics (counters)
%%-----------------------------------------------------------------

reset_stats() ->
    megaco_stats:reset_stats(megaco_stats).

reset_stats(ConnHandleOrCounter) ->
    megaco_stats:reset_stats(megaco_stats, ConnHandleOrCounter).



%%----------------------------------------------------------------------
%% cleanup utility functions
%%----------------------------------------------------------------------

cleanup(#megaco_conn_handle{local_mid = LocalMid}, Force) 
  when (Force =:= true) orelse (Force =:= false) ->
    Pat = #reply{trans_id  = '$1', 
		 local_mid = LocalMid, 
		 state     = '$2',
		 _         = '_'},
    do_cleanup(Pat, Force);
cleanup(LocalMid, Force) 
  when (Force =:= true) orelse (Force =:= false) ->
    Pat = #reply{trans_id  = '$1', 
		 local_mid = LocalMid, 
		 state     = '$2',
		 _         = '_'},
    do_cleanup(Pat, Force).
    
do_cleanup(Pat, Force) ->
    Match = megaco_monitor:which_replies(Pat),
    Reps  = [{V1, V2} || [V1, V2] <- Match],
    do_cleanup2(Reps, Force).

do_cleanup2([], _) ->
    ok;
do_cleanup2([{TransId, aborted}|T], Force = false) ->
    megaco_monitor:delete_reply(TransId),
    do_cleanup2(T, Force);
do_cleanup2([_|T], Force = false) ->
    do_cleanup2(T, Force);
do_cleanup2([{TransId, _State}|T], Force = true) ->
    megaco_monitor:delete_reply(TransId),
    do_cleanup2(T, Force).
		  

%%----------------------------------------------------------------------
%% which_requests and which_replies utility functions
%%----------------------------------------------------------------------

which_requests(#megaco_conn_handle{local_mid  = LocalMid, 
				   remote_mid = RemoteMid}) ->
    Pat1 = #trans_id{mid    = LocalMid, 
		     serial = '$1', _ = '_'},
    Pat2 = #request{trans_id   = Pat1, 
		    remote_mid = RemoteMid, 
		    _ = '_'},
    Match = megaco_monitor:which_requests(Pat2),
    [S || [S] <- Match];
which_requests(LocalMid) ->
    Pat1 = #trans_id{mid    = LocalMid, 
		     serial = '$1', _ = '_'},
    Pat2 = #request{trans_id   = Pat1, 
		    remote_mid = '$2', _ = '_'},
    Match0 = megaco_monitor:which_requests(Pat2),
    Match1 = [{mk_ch(LocalMid, V2), V1} || [V1, V2] <- Match0],
    which_requests1(lists:sort(Match1)).

which_requests1([]) ->
    [];
which_requests1([{CH, S}|T]) ->
    which_requests2(T, CH, [S], []).

which_requests2([], CH, Serials, Reqs) ->
    lists:reverse([{CH, Serials}|Reqs]);
which_requests2([{CH, S}|T], CH, Serials, Reqs) ->
    which_requests2(T, CH, [S|Serials], Reqs);
which_requests2([{CH1, S}|T], CH2, Serials, Reqs) ->
    which_requests2(T, CH1, [S], [{CH2, lists:reverse(Serials)}| Reqs]).
    

which_replies(#megaco_conn_handle{local_mid  = LocalMid, 
				  remote_mid = RemoteMid}) ->
    Pat1 = #trans_id{mid    = RemoteMid, 
		     serial = '$1', _ = '_'},
    Pat2 = #reply{trans_id  = Pat1, 
  		  local_mid = LocalMid, 
  		  state     = '$2', 
  		  handler   = '$3', _ = '_'},
    Match = megaco_monitor:which_replies(Pat2),
    [{V1, V2, V3} || [V1, V2, V3] <- Match];
which_replies(LocalMid) ->
    Pat1 = #trans_id{mid    = '$1', 
		     serial = '$2', _ = '_'},
    Pat2 = #reply{trans_id  = Pat1, 
		  local_mid = LocalMid, 
		  state     = '$3', 
		  handler   = '$4', _ = '_'},
    Match0 = megaco_monitor:which_replies(Pat2),
    Match1 = [{mk_ch(LocalMid,V1),{V2,V3,V4}} || [V1, V2, V3, V4] <- Match0],
    which_replies1(lists:sort(Match1)).

which_replies1([]) ->
    [];
which_replies1([{CH, Data}|T]) ->
    which_replies2(T, CH, [Data], []).

which_replies2([], CH, Data, Reps) ->
    lists:reverse([{CH, Data}|Reps]);
which_replies2([{CH, Data}|T], CH, Datas, Reps) ->
    which_replies2(T, CH, [Data|Datas], Reps);
which_replies2([{CH1, Data}|T], CH2, Datas, Reps) ->
    which_replies2(T, CH1, [Data], [{CH2, lists:reverse(Datas)}| Reps]).
    
    
mk_ch(LM, RM) ->
    #megaco_conn_handle{local_mid = LM, remote_mid = RM}.
    

%%----------------------------------------------------------------------
%% Register/unreister connections
%%----------------------------------------------------------------------

%% Returns {ok, ConnHandle} | {error, Reason}
autoconnect(RH, RemoteMid, SendHandle, ControlPid, Extra)
  when is_record(RH, megaco_receive_handle) ->
    ?rt2("autoconnect", [RH, RemoteMid, SendHandle, ControlPid]),
    case megaco_config:autoconnect(RH, RemoteMid, SendHandle, ControlPid) of
        {ok, ConnData} ->
            do_connect(ConnData, Extra);
        {error, Reason} ->
            {error, Reason}
    end;
autoconnect(BadHandle, _CH, _SendHandle, _ControlPid, _Extra) ->
    {error, {bad_receive_handle, BadHandle}}.

connect(RH, RemoteMid, SendHandle, ControlPid) ->
    Extra = ?default_user_callback_extra, 
    connect(RH, RemoteMid, SendHandle, ControlPid, Extra).
connect(RH, RemoteMid, SendHandle, ControlPid, Extra)
  when is_record(RH, megaco_receive_handle) ->
    ?rt2("connect", [RH, RemoteMid, SendHandle, ControlPid, Extra]),

    %% The purpose of this is to have a temoporary process, to 
    %% which one can set up a monitor or link and get a 
    %% notification when process exits. The entire connect is 
    %% done in the temporary worker process. 
    %% When it exits, the connect is either successfully done 
    %% or it failed.

    ConnectorFun = 
	fun() ->

		ConnectResult = 
		    case megaco_config:connect(RH, RemoteMid, 
					       SendHandle, ControlPid) of
			{ok, ConnData} ->
			    do_connect(ConnData, Extra);
			{error, Reason} ->
			    {error, Reason}
		    end,
		?rt2("connector: connected", [self(), ConnectResult]),
		exit({result, ConnectResult})
	end,
    Flag      = process_flag(trap_exit, true),
    Connector = erlang:spawn_link(ConnectorFun),
    receive
	{'EXIT', Connector, {result, ConnectResult}} ->
	    ?rt2("connect result: received expected connector exit signal", 
		 [Connector, ConnectResult]),
	    process_flag(trap_exit, Flag),
	    ConnectResult;
	{'EXIT', Connector, OtherReason} ->
	    ?rt2("connect exit: received unexpected connector exit signal", 
		 [Connector, OtherReason]),
	    process_flag(trap_exit, Flag),
	    {error, OtherReason}
    end;
connect(BadHandle, _CH, _SendHandle, _ControlPid, _Extra) ->
    {error, {bad_receive_handle, BadHandle}}.

do_connect(CD, Extra) ->
    CH       = CD#conn_data.conn_handle,
    Version  = CD#conn_data.protocol_version,
    UserMod  = CD#conn_data.user_mod,
    UserArgs = CD#conn_data.user_args,
    Args      = 
	case Extra of
	    ?default_user_callback_extra ->
		[CH, Version | UserArgs];
	    _ ->
		[CH, Version, Extra | UserArgs]
	end,
    ?report_trace(CD, "callback: connect", [Args]),
    Res = (catch apply(UserMod, handle_connect, Args)),
    ?report_debug(CD, "return: connect", [{return, Res}]),
    case Res of
        ok ->
	    ?SIM(ok, do_connect), % do_encode),
	    monitor_process(CH, CD#conn_data.control_pid);
        error ->
            megaco_config:disconnect(CH),
            {error, {connection_refused, CD, error}};
        {error, ED} when is_record(ED,'ErrorDescriptor') ->
            megaco_config:disconnect(CH),
            {error, {connection_refused, CD, ED}};
        _Error ->
	    warning_msg("connect callback failed: ~w", [Res]),
            megaco_config:disconnect(CH),
            {error, {connection_refused, CD, Res}}
    end.

finish_connect(#conn_data{control_pid = ControlPid} = CD) 
  when is_pid(ControlPid) andalso (node(ControlPid) =:= node()) ->
    ?rt1(CD, "finish local connect", [ControlPid]),
    do_finish_connect(CD);
finish_connect(#conn_data{conn_handle = CH,
			  control_pid = ControlPid} = CD) 
  when is_pid(ControlPid) andalso (node(ControlPid) =/= node()) ->
    ?rt1(CD, "finish remote connect", [ControlPid]),
    RemoteNode     = node(ControlPid),
    UserMonitorPid = whereis(megaco_monitor),
    Args           = [CH, ControlPid, UserMonitorPid],
    case rpc:call(RemoteNode, ?MODULE, connect_remote, Args) of
        {ok, ControlMonitorPid} ->
	    do_finish_connect(CD#conn_data{control_pid = ControlMonitorPid});
        {error, Reason} ->
            disconnect(CH, {connect_remote, Reason}),
            {error, Reason};
        {badrpc, Reason} ->
            Reason2 = {'EXIT', Reason},
            disconnect(CH, {connect_remote, Reason2}),
            {error, Reason2}
    end.

do_finish_connect(#conn_data{conn_handle = CH,
			     send_handle = SendHandle,
			     control_pid = ControlPid} = CD) ->
    M   = ?MODULE,
    F   = disconnect_local,
    A   = [CH],
    MFA = {M, F, A}, 
    case megaco_config:finish_connect(CH, SendHandle, ControlPid, MFA) of
	{ok, Ref} ->
	    {ok, CD#conn_data{monitor_ref = Ref}};
	{error, Reason} ->
            {error, {config_update, Reason}}
    end.


monitor_process(CH, ControlPid) 
  when is_pid(ControlPid) andalso (node(ControlPid) =:= node()) ->
    M = ?MODULE,
    F = disconnect_local,
    A = [CH],
    Ref = megaco_monitor:apply_at_exit(M, F, A, ControlPid),
    case megaco_config:update_conn_info(CH, monitor_ref, Ref) of
        ok ->
            ?SIM({ok, CH}, monitor_process_local);
        {error, Reason} ->
            disconnect(CH, {config_update, Reason}),
            {error, Reason}
    end;
monitor_process(CH, ControlPid) 
  when is_pid(ControlPid) andalso (node(ControlPid) =/= node()) ->
    RemoteNode = node(ControlPid),
    UserMonitorPid = whereis(megaco_monitor),
    Args = [CH, ControlPid, UserMonitorPid],
    case rpc:call(RemoteNode, ?MODULE, connect_remote, Args) of
        {ok, ControlMonitorPid} ->
            M = ?MODULE,
            F = disconnect_local,
            A = [CH],
            Ref = megaco_monitor:apply_at_exit(M, F, A, ControlMonitorPid),
            case megaco_config:update_conn_info(CH, monitor_ref, Ref) of
                ok ->
		    ?SIM({ok, CH}, monitor_process_remote);
                {error, Reason} ->
                    disconnect(CH, {config_update, Reason}),
                    {error, Reason}
            end;
        {error, Reason} ->
            disconnect(CH, {connect_remote, Reason}),
            {error, Reason};
        {badrpc, Reason} ->
            Reason2 = {'EXIT', Reason},
            disconnect(CH, {connect_remote, Reason2}),
            {error, Reason2}
    end;
monitor_process(CH, undefined = _ControlPid) ->
    %% We have to do this later (setting up the monitor), 
    %% when the first message arrives. The 'connected' atom is
    %% the indication for the first arriving message to finish
    %% the connect. 
    %% This may be the case when an MGC performs a pre-connect
    %% in order to speed up the handling of an (expected) connecting
    %% MG. 
    case megaco_config:update_conn_info(CH, monitor_ref, connected) of
        ok ->
            ?SIM({ok, CH}, monitor_process_local);
        {error, Reason} ->
            disconnect(CH, {config_update, Reason}),
            {error, Reason}
    end.

connect_remote(CH, ControlPid, UserMonitorPid)
  when node(ControlPid) =:= node() andalso node(UserMonitorPid) =/= node() ->
    case megaco_config:lookup_local_conn(CH) of
        [_ConnData] -> 
            UserNode = node(UserMonitorPid),
            M = ?MODULE,
            F = disconnect_remote,
            A = [CH, UserNode],
            Ref = megaco_monitor:apply_at_exit(M, F, A, UserMonitorPid),
            case megaco_config:connect_remote(CH, UserNode, Ref) of
		ok ->
		    ControlMonitorPid = whereis(megaco_monitor),
		    ?SIM({ok, ControlMonitorPid}, connect_remote);
		{error, Reason} ->
		    {error, Reason}
	    end;
        [] ->
            {error, {no_connection, CH}}
    end.

cancel_apply_at_exit({connecting, _ConnectorPid}) ->
    ok;
cancel_apply_at_exit(connected) ->
    ok;
cancel_apply_at_exit(ControlRef) ->
    megaco_monitor:cancel_apply_at_exit(ControlRef).

node_of_control_pid(Pid) when is_pid(Pid) ->
    node(Pid);
node_of_control_pid(_) ->
    node().

disconnect(ConnHandle, DiscoReason)
  when is_record(ConnHandle, megaco_conn_handle) ->
    case megaco_config:disconnect(ConnHandle) of
        {ok, ConnData, RemoteConnData} ->
            ControlRef = ConnData#conn_data.monitor_ref,
            cancel_apply_at_exit(ControlRef),
            handle_disconnect_callback(ConnData, DiscoReason),
            ControlNode = node_of_control_pid(ConnData#conn_data.control_pid),
            case ControlNode =:= node() of
                true ->
                    %% Propagate to remote users
                    CancelFun =
                        fun(RCD) ->
                                UserRef = RCD#remote_conn_data.monitor_ref,
                                cancel_apply_at_exit(UserRef),
                                RCD#remote_conn_data.user_node
                          end,
                    Nodes = lists:map(CancelFun, RemoteConnData),
		    %% io:format("NODES: ~p~n", [Nodes]),
                    M = ?MODULE,
                    F = disconnect,
                    A = [ConnHandle, DiscoReason],
                    case rpc:multicall(Nodes, M, F, A) of
                        {Res, []} ->
			    Check = fun(ok) -> false;
				       ({error, {no_connection, _CH}}) -> false;
				       (_) -> true
				    end,
                            case lists:filter(Check, Res) of
                                [] ->
                                    ok;
                                Bad ->
                                    {error, {remote_disconnect_error, ConnHandle, Bad}}
                            end;
                        {_Res, Bad} ->
                            {error, {remote_disconnect_crash, ConnHandle, Bad}}
                    end;
                false when (RemoteConnData =:= []) ->
                    %% Propagate to remote control node
                    M = ?MODULE,
                    F = disconnect_remote,
                    A = [DiscoReason, ConnHandle, node()],
                    case rpc:call(ControlNode, M, F, A) of
                        {badrpc, Reason} ->
                            {error, {'EXIT', Reason}};
                        Other ->
                            Other
                    end
            end;
        {error, Reason} ->
            {error, Reason}
    end;
disconnect(BadHandle, Reason) ->
    {error, {bad_conn_handle, BadHandle, Reason}}.

disconnect_local(Reason, ConnHandle) ->
    disconnect(ConnHandle, {no_controlling_process, Reason}).

disconnect_remote(_Reason, ConnHandle, UserNode) ->
    case megaco_config:disconnect_remote(ConnHandle, UserNode) of
        [RCD] ->
            Ref = RCD#remote_conn_data.monitor_ref,
            cancel_apply_at_exit(Ref),
            ok;
        [] ->
            {error, {no_connection, ConnHandle}}
    end.


%%----------------------------------------------------------------------
%% Handle incoming message
%%----------------------------------------------------------------------

receive_message(ReceiveHandle, ControlPid, SendHandle, Bin) ->
    Extra = ?default_user_callback_extra, 
    receive_message(ReceiveHandle, ControlPid, SendHandle, Bin, Extra).

receive_message(ReceiveHandle, ControlPid, SendHandle, Bin, Extra) ->
    Opts = [link , {min_heap_size, 5000}],
    spawn_opt(?MODULE,
               process_received_message,
               [ReceiveHandle, ControlPid, SendHandle, Bin, self(), Extra], Opts),
    ok.

%% This function is called via the spawn_opt function with the link
%% option, therefor the unlink before the exit.
process_received_message(ReceiveHandle, ControlPid, SendHandle, Bin, Receiver, 
			 Extra) ->
    process_received_message(ReceiveHandle, ControlPid, SendHandle, Bin, Extra),
    unlink(Receiver),
    exit(normal).

process_received_message(ReceiveHandle, ControlPid, SendHandle, Bin) ->
    Extra = ?default_user_callback_extra, 
    process_received_message(ReceiveHandle, ControlPid, SendHandle, Bin, Extra).

process_received_message(ReceiveHandle, ControlPid, SendHandle, Bin, Extra) ->
    Flag = process_flag(trap_exit, true),
    case prepare_message(ReceiveHandle, SendHandle, Bin, ControlPid, Extra) of
        {ok, ConnData, MegaMsg} when is_record(MegaMsg, 'MegacoMessage') ->
	    ?rt1(ConnData, "message prepared", [MegaMsg]),
            Mess = MegaMsg#'MegacoMessage'.mess,
            case Mess#'Message'.messageBody of
                {transactions, Transactions} ->
                    {AckList, ReqList} = 
			prepare_trans(ConnData, Transactions, [], [], Extra),
                    handle_acks(AckList, Extra),
		    case ReqList of
			[] ->
			    ?rt3("no transaction requests"),
			    ignore;
			[Req|Reqs] when (ConnData#conn_data.threaded =:= true) ->
			    ?rt3("handle requests (spawned)"),
 			    lists:foreach(
 			      fun(R) -> 
 				      spawn(?MODULE, handle_request, [R, Extra]) 
 			      end, 
 			      Reqs),
			    handle_request(Req, Extra);
			_ ->
			    ?rt3("handle requests"),
			    case handle_requests(ReqList, [], Extra) of
				[] ->
				    ignore;
				[LongRequest | More] ->
				    lists:foreach(
				      fun(LR) ->
					      spawn(?MODULE, handle_long_request, [LR, Extra])
				      end,
				      More),
				    handle_long_request(LongRequest, Extra)
			    end
		    end;
                {messageError, Error} ->
                    handle_message_error(ConnData, Error, Extra)
            end;
        {silent_fail, ConnData, {_Code, Reason, Error}} ->
            ?report_debug(ConnData, Reason, [no_reply, Error]),
            ignore;
        {verbose_fail, ConnData, {Code, Reason, Error}} ->
            ?report_debug(ConnData, Reason, [Error]),
            send_message_error(ConnData, Code, Reason)
    end,
    process_flag(trap_exit, Flag),
    ok.

prepare_message(RH, SH, Bin, Pid, Extra)
  when is_record(RH, megaco_receive_handle) andalso is_pid(Pid) ->
    ?report_trace(RH, "receive bytes", [{bytes, Bin}]),
    EncodingMod    = RH#megaco_receive_handle.encoding_mod,
    EncodingConfig = RH#megaco_receive_handle.encoding_config,
    ProtVersion    = RH#megaco_receive_handle.protocol_version,
    case (catch EncodingMod:decode_message(EncodingConfig, ProtVersion, Bin)) of
        {ok, MegaMsg} when is_record(MegaMsg, 'MegacoMessage') ->
	    ?report_trace(RH, "receive message", [{message, MegaMsg}]),
            Mess       = MegaMsg#'MegacoMessage'.mess,
            RemoteMid  = Mess#'Message'.mId,
            Version    = Mess#'Message'.version,
            LocalMid   = RH#megaco_receive_handle.local_mid,
            CH         = #megaco_conn_handle{local_mid  = LocalMid,
                                             remote_mid = RemoteMid},
            case megaco_config:lookup_local_conn(CH) of

		%% 
		%% Message is not of the negotiated version
		%% 

                [#conn_data{protocol_version = NegVersion, 
			    strict_version   = true} = ConnData] 
		when NegVersion =/= Version ->
		    %% Use already established connection, 
		    %% but incorrect version
		    ?rt1(ConnData, "not negotiated version", [Version]),
		    Error = {error, {not_negotiated_version, 
				     NegVersion, Version}}, 
		    handle_syntax_error_callback(RH, ConnData, 
						 prepare_error(Error),
						 Extra);


                [ConnData] ->

		    %% 
                    %% Use an already established connection
		    %% 
		    %% This *may* have been set up in the
		    %% "non-official" way, so we may need to 
		    %% create the monitor to the control process
		    %% and store the SendHandle (which is normally 
		    %% done when creating the "temporary" connection). 
		    %% 

		    ?rt1(ConnData, "use already established connection", []),
                    ConnData2 = ConnData#conn_data{send_handle      = SH,
						   control_pid      = Pid, 
                                                   protocol_version = Version},
                    check_message_auth(CH, ConnData2, MegaMsg, Bin);

                [] ->
                    %% Setup a temporary connection
		    ?rt3("setup a temporary connection"),
                    case autoconnect(RH, RemoteMid, SH, Pid, Extra) of
                        {ok, _} ->
			    do_prepare_message(RH, CH, SH, MegaMsg, Pid, Bin);
			{error, {already_connected, _ConnHandle}} ->
			    do_prepare_message(RH, CH, SH, MegaMsg, Pid, Bin);
			{error, {connection_refused, ConnData, Reason}} ->
			    Error = prepare_error({error, {connection_refused, Reason}}),
                            {verbose_fail, ConnData, Error};
                        {error, Reason} ->
                            ConnData = fake_conn_data(RH, RemoteMid, SH, Pid),
			    ConnData2 = ConnData#conn_data{protocol_version = Version},
			    Error    = prepare_error({error, Reason}),
                            {verbose_fail, ConnData2, Error}
                    end
            end;
        Error ->
	    ?rt2("decode error", [Error]),
	    ConnData = handle_decode_error(Error, 
					   RH, SH, Bin, Pid, 
					   EncodingMod, 
					   EncodingConfig, 
					   ProtVersion),
            handle_syntax_error_callback(RH, ConnData, prepare_error(Error), Extra)
    end;
prepare_message(RH, SendHandle, _Bin, ControlPid, _Extra) ->
    ConnData = fake_conn_data(RH, SendHandle, ControlPid),
    Error    = prepare_error({'EXIT', {bad_receive_handle, RH}}),
    {verbose_fail, ConnData, Error}.


handle_decode_error({error, {unsupported_version, _}},
		    #megaco_receive_handle{local_mid = LocalMid} = RH, SH, 
		    Bin, Pid,
		    EM, EC, V) ->
    case (catch EM:decode_mini_message(EC, V, Bin)) of
	{ok, #'MegacoMessage'{mess = #'Message'{version = _Ver, 
						mId     = RemoteMid}}} ->
	    ?rt2("erroneous message received", [SH, RemoteMid, _Ver]),
            CH = #megaco_conn_handle{local_mid  = LocalMid,
				     remote_mid = RemoteMid},
	    incNumErrors(CH),
	    %% We cannot put the version into conn-data, that will
	    %% make the resulting error message impossible to sent
	    %% (unsupported version)
	    case megaco_config:lookup_local_conn(CH) of
                [ConnData] ->
		    ?rt3("known to us"),
		    ConnData#conn_data{send_handle = SH};
		[] ->
		    ?rt3("unknown to us"),
		    ConnData = fake_conn_data(RH, SH, Pid),
		    ConnData#conn_data{conn_handle = CH}
	    end;

	_ ->
	    ?rt2("erroneous message received", [SH]),
	    incNumErrors(),
	    fake_conn_data(RH, SH, Pid)
    end;

handle_decode_error(_,
		    #megaco_receive_handle{local_mid = LocalMid} = RH, SH, 
		    Bin, Pid,
		    EM, EC, V) ->
    case (catch EM:decode_mini_message(EC, V, Bin)) of
	{ok, #'MegacoMessage'{mess = #'Message'{version = Ver, 
						mId     = RemoteMid}}} ->
	    ?rt2("erroneous message received", [SH, Ver, RemoteMid]),
            CH = #megaco_conn_handle{local_mid  = LocalMid,
				     remote_mid = RemoteMid},
	    incNumErrors(CH),
	    case megaco_config:lookup_local_conn(CH) of
                [ConnData] ->
		    ?rt3("known to us"),
		    ConnData#conn_data{send_handle      = SH,
				       protocol_version = Ver};
		[] ->
		    ?rt3("unknown to us"),
		    ConnData = fake_conn_data(RH, SH, Pid),
		    ConnData#conn_data{conn_handle      = CH,
				       protocol_version = Ver}
	    end;

	_ ->
	    ?rt2("erroneous message received", [SH]),
	    incNumErrors(),
	    fake_conn_data(RH, SH, Pid)
    end.


do_prepare_message(RH, CH, SendHandle, MegaMsg, ControlPid, Bin) ->
    case megaco_config:lookup_local_conn(CH) of
	[ConnData] ->
	    case check_message_auth(CH, ConnData, MegaMsg, Bin) of
		{ok, ConnData2, MegaMsg} ->
		    %% Let the connection be permanent
		    {ok, ConnData2, MegaMsg};
		{ReplyTag, ConnData, Reason} ->
		    %% Remove the temporary connection
		    disconnect(CH, {bad_auth, Reason}),
		    {ReplyTag, ConnData, Reason}
	    end;
	[] ->
	    Reason = no_connection,
	    disconnect(CH, Reason),
	    RemoteMid = CH#megaco_conn_handle.remote_mid,
	    ConnData = fake_conn_data(RH, RemoteMid, SendHandle, ControlPid),
	    Error = prepare_error({error, Reason}),
	    {silent_fail, ConnData, Error}
    end.

check_message_auth(_ConnHandle, ConnData, MegaMsg, Bin) ->
    MsgAuth   = MegaMsg#'MegacoMessage'.authHeader,
    Mess      = MegaMsg#'MegacoMessage'.mess,
    Version   = Mess#'Message'.version,
    ConnData2 = ConnData#conn_data{protocol_version = Version},
    ConnAuth  = ConnData2#conn_data.auth_data,
    ?report_trace(ConnData2, "check message auth", [{bytes, Bin}]),
    if
	(MsgAuth =:= asn1_NOVALUE) andalso (ConnAuth =:= asn1_NOVALUE) ->
            ?SIM({ok, ConnData2, MegaMsg}, check_message_auth);
	true -> 
	    ED = #'ErrorDescriptor'{errorCode = ?megaco_unauthorized,
				    errorText = "Autentication is not supported"},
	    {verbose_fail, ConnData2, prepare_error({error, ED})}
    end.

handle_syntax_error_callback(ReceiveHandle, ConnData, PrepError, Extra) ->
    {Code, Reason, Error} = PrepError,
    ErrorDesc = #'ErrorDescriptor'{errorCode = Code, errorText = Reason},
    Version   = 
	case Error of
	    {error, {unsupported_version, UV}} ->
		UV;
	    _ ->
		ConnData#conn_data.protocol_version
	end,
    UserMod   = ConnData#conn_data.user_mod,
    UserArgs  = ConnData#conn_data.user_args,
    ?report_trace(ReceiveHandle, "callback: syntax error", [ErrorDesc, Error]),
    Args      = 
	case Extra of
	    ?default_user_callback_extra ->
		[ReceiveHandle, Version, ErrorDesc | UserArgs];
	    _ ->
		[ReceiveHandle, Version, ErrorDesc, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_syntax_error, Args)),
    ?report_debug(ReceiveHandle, "return: syntax error", 
		  [{return, Res}, ErrorDesc, Error]),
    case Res of
        reply ->
            {verbose_fail, ConnData, PrepError};
        {reply,#'ErrorDescriptor'{errorCode = Code1, errorText = Reason1}} ->
            {verbose_fail, ConnData, {Code1,Reason1,Error}};
        no_reply ->
            {silent_fail, ConnData, PrepError};
        {no_reply,#'ErrorDescriptor'{errorCode=Code2,errorText=Reason2}} ->
            {silent_fail, ConnData, {Code2,Reason2,Error}}; %%% OTP-????
        _ ->
	    warning_msg("syntax error callback failed: ~w", [Res]),
            {verbose_fail, ConnData, PrepError}
    end.

fake_conn_data(CH) when is_record(CH, megaco_conn_handle) ->
    case (catch megaco_config:conn_info(CH, receive_handle)) of
	RH when is_record(RH, megaco_receive_handle) ->
	    RemoteMid = CH#megaco_conn_handle.remote_mid,
	    ConnData = 
		fake_conn_data(RH, RemoteMid, no_send_handle, no_control_pid),
	    ConnData#conn_data{conn_handle = CH};
	{'EXIT', _} ->
	    UserMid = CH#megaco_conn_handle.local_mid,
	    case catch megaco_config:user_info(UserMid, receive_handle) of
		{'EXIT', _} -> % No such user
		    #conn_data{conn_handle        = CH,
			       serial             = undefined_serial,
			       control_pid        = no_control_pid,
			       monitor_ref        = undefined_monitor_ref,
			       send_mod           = no_send_mod,
			       send_handle        = no_send_handle,
			       encoding_mod       = no_encoding_mod,
			       encoding_config    = no_encoding_config,
			       reply_action       = undefined,
			       sent_pending_limit = infinity,
			       recv_pending_limit = infinity};
		RH ->
		    ConnData = 
			fake_conn_data(RH, no_send_handle, no_control_pid),
		    ConnData#conn_data{conn_handle = CH}
	    end
    end.

fake_conn_data(RH, SendHandle, ControlPid) ->
    fake_conn_data(RH, unknown_remote_mid, SendHandle, ControlPid).

fake_conn_data(RH, RemoteMid, SendHandle, ControlPid) ->
    case catch megaco_config:init_conn_data(RH, RemoteMid, SendHandle, ControlPid) of
	{'EXIT', _} -> % No such user
	    fake_user_data(RH, RemoteMid, SendHandle, ControlPid);
	ConnData ->
	    ConnData
    end.

fake_user_data(RH, RemoteMid, SendHandle, ControlPid) ->
    LocalMid = RH#megaco_receive_handle.local_mid,
    RH2 = RH#megaco_receive_handle{local_mid = default},
    case catch megaco_config:init_conn_data(RH2, RemoteMid, SendHandle, ControlPid) of
	{'EXIT', _} -> % Application stopped?
	    ConnHandle     = #megaco_conn_handle{local_mid  = LocalMid,
						 remote_mid = RemoteMid},
	    EncodingMod    = RH#megaco_receive_handle.encoding_mod,
	    EncodingConfig = RH#megaco_receive_handle.encoding_config,
	    SendMod        = RH#megaco_receive_handle.send_mod,
	    #conn_data{conn_handle        = ConnHandle,
		       serial             = undefined_serial,
		       control_pid        = ControlPid,
		       monitor_ref        = undefined_monitor_ref,
		       send_mod           = SendMod,
		       send_handle        = SendHandle,
		       encoding_mod       = EncodingMod,
		       encoding_config    = EncodingConfig,
		       reply_action       = undefined,
		       sent_pending_limit = infinity,
		       recv_pending_limit = infinity};
	ConnData ->
	    ConnData
    end.

prepare_error(Error) ->
    case Error of
        {error, ED} when is_record(ED, 'ErrorDescriptor') ->
            Code   = ED#'ErrorDescriptor'.errorCode,
            Reason = ED#'ErrorDescriptor'.errorText,
            {Code, Reason, Error};
        {error, [{reason, {bad_token, [BadToken, _Acc]}, Line}]} when is_integer(Line) ->
            Reason = 
		lists:flatten(
		  io_lib:format("Illegal token (~p) on line ~w", [BadToken, Line])),
            Code = ?megaco_bad_request,
            {Code, Reason, Error};
        {error, [{reason, {bad_token, _}, Line}]} when is_integer(Line) ->
            Reason = lists:concat(["Illegal token on line ", Line]),
            Code = ?megaco_bad_request,
            {Code, Reason, Error};
        {error, [{reason, {Line, _ParserMod, RawReasonString}} | _]} when is_integer(Line) andalso is_list(RawReasonString) ->
	    Reason = 
		case RawReasonString of
		    [[$s, $y, $n, $t, $a, $x | _], TokenString] ->
			lists:flatten(
			  io_lib:format("Syntax error on line ~w before token ~s", [Line, TokenString]));
		    _ ->
			lists:flatten(io_lib:format("Syntax error on line ~w", [Line]))
		end,
            Code = ?megaco_bad_request,
            {Code, Reason, Error};
        {error, [{reason, {Line, _, _}} | _]} when is_integer(Line) ->
            Reason = lists:concat(["Syntax error on line ", Line]),
            Code = ?megaco_bad_request,
            {Code, Reason, Error};
        {error, {connection_refused, ED}} when is_record(ED,'ErrorDescriptor') ->
            Code   = ED#'ErrorDescriptor'.errorCode,
            Reason = ED#'ErrorDescriptor'.errorText,
            {Code, Reason, Error};
        {error, {connection_refused, _}} ->
            Reason = "Connection refused by user",
            Code = ?megaco_unauthorized,
            {Code, Reason, Error};
        {error, {unsupported_version, V}} ->
            Reason = 
		lists:flatten(io_lib:format("Unsupported version: ~w",[V])),
            Code = ?megaco_version_not_supported, 
            {Code, Reason, Error};
        {error, {not_negotiated_version, NegV, MsgV}} ->
            Reason = 
		lists:flatten(
		  io_lib:format("Not negotiated version: ~w [negotiated ~w]",
				[MsgV, NegV])),
            Code = ?megaco_version_not_supported, 
            {Code, Reason, Error};
        {error, _} ->
            Reason = "Syntax error",
            Code = ?megaco_bad_request,
            {Code, Reason, Error};
        {ok, MegaMsg} when is_record(MegaMsg, 'MegacoMessage') ->
            Reason = "MID does not match config",
            Code = ?megaco_incorrect_identifier,
            {Code, Reason, Error};
        _ ->
            Reason = "Fatal syntax error",
            Code = ?megaco_internal_gateway_error,
            {Code, Reason, Error}
    end.

prepare_trans(_ConnData, [], AckList, ReqList, _Extra) ->
    ?SIM({AckList, ReqList}, prepare_trans_done);

prepare_trans(ConnData, Trans, AckList, ReqList, Extra) 
  when ConnData#conn_data.monitor_ref =:= undefined_auto_monitor_ref ->

    ?rt3("prepare_trans - autoconnect"),

    %% <BUGBUG>
    %% Do we need something here, if we send more then one 
    %% trans per message?
    %% </BUGBUG>
    
    %% May occur if another process has already setup a
    %% temporary connection, but the handle_connect callback
    %% function has not yet returned before the eager MG
    %% re-sends its initial service change message.

    prepare_autoconnecting_trans(ConnData, Trans, AckList, ReqList, Extra);

prepare_trans(#conn_data{monitor_ref = connected} = ConnData, 
	      Trans, AckList, ReqList, Extra) ->

    ?rt3("prepare_trans - connected"),

    %% 
    %% This will happen when the "MGC" user performs a "pre" connect,
    %% instead of waiting for the auto-connect (which normally
    %% happen when the MGC receives the first message from the
    %% MG). 
    %% 

    %% 
    %% The monitor_ref will have this value when the pre-connect 
    %% is complete, so we finish it here and then continue with the 
    %% normal transaction prepare.
    %% 

    case finish_connect(ConnData) of
	{ok, CD} ->
	    prepare_normal_trans(CD, Trans, AckList, ReqList, Extra);
	{error, Reason} ->
	    disconnect(ConnData#conn_data.conn_handle, Reason),
	    {[], []}
    end;

prepare_trans(#conn_data{monitor_ref = {connecting, _}} = _ConnData, 
	      _Trans, _AckList, _ReqList, _Extra) ->
    
    ?rt3("prepare_trans - connecting"),

    %% 
    %% This will happen when the "MGC" user performs a "pre" connect,
    %% instead of waiting for the auto-connect (which normally
    %% happen when the MGC receives the first message from the
    %% MG). 
    %% 

    %% 
    %% The monitor_ref will have this value when the pre-connect 
    %% is in progress. We drop (ignore) this message and hope the
    %% other side (MG) will resend. 
    %% 

    %% prepare_connecting_trans(ConnData, Trans, AckList, ReqList, Extra);
    {[], []};

prepare_trans(ConnData, Trans, AckList, ReqList, Extra) ->

    ?rt3("prepare_trans - normal"),

    %% Handle transaction in the normal case

    prepare_normal_trans(ConnData, Trans, AckList, ReqList, Extra).


prepare_autoconnecting_trans(_ConnData, [], AckList, ReqList, _Extra) ->
    ?SIM({AckList, ReqList}, prepare_autoconnecting_trans_done);

prepare_autoconnecting_trans(ConnData, [Trans | Rest], AckList, ReqList, Extra) ->
    ?rt1(ConnData, "[autoconnecting] prepare trans", [Trans]),
    case Trans of
        {transactionRequest, T} when is_record(T, 'TransactionRequest') ->
	    
            Serial = T#'TransactionRequest'.transactionId,
	    ConnData2 = ConnData#conn_data{serial = Serial},
	    ?report_trace(ConnData2, "Pending handle_connect", [T]),

	    %% ------------------------------------------
	    %% 
	    %%   Check pending limit
	    %% 
	    %% ------------------------------------------

	    Limit = ConnData#conn_data.sent_pending_limit,
	    TransId = to_remote_trans_id(ConnData2),
	    case check_and_maybe_incr_pending_limit(Limit, sent, TransId) of
		ok ->
		    send_pending(ConnData2);
		error ->
		    %% Pending limit:
		    %% In this (granted, highly hypothetical case)
		    %% we would make the user very confused if we 
		    %% called the abort callback function, since 
		    %% the request callback function has not yet
		    %% been called. Alas, we skip this call here.
		    send_pending_limit_error(ConnData);
		aborted ->
		    ignore
	    end,
	    prepare_autoconnecting_trans(ConnData2, Rest, AckList, ReqList, 
					 Extra);
	_ ->
	    prepare_autoconnecting_trans(ConnData, Rest, AckList, ReqList, 
					 Extra)
    end.


%% =================================================================
%% 
%% Note that the TransactionReply record was changed i v3 (two 
%% new fields where added), and since we don't know which version,
%% we cannot use the record definition of TransactionReply.
%% Instead we transform the record into our own internal format
%% #megaco_transaction_reply{}
%%
%% =================================================================

prepare_normal_trans(_ConnData, [], AckList, ReqList, _Extra) ->
    ?SIM({AckList, ReqList}, prepare_normal_trans_done);

prepare_normal_trans(ConnData, [Trans | Rest], AckList, ReqList, Extra) ->
    ?rt1(ConnData, "prepare [normal] trans", [Trans]),
    case Trans of
        {transactionRequest, #'TransactionRequest'{transactionId = asn1_NOVALUE}} ->
            ConnData2 = ConnData#conn_data{serial = 0},
	    Code   = ?megaco_bad_request,
            Reason = "Syntax error in message: transaction id missing",
	    send_trans_error(ConnData2, Code, Reason),
            prepare_normal_trans(ConnData2, Rest, AckList, ReqList, Extra);
        {transactionRequest, T} when is_record(T, 'TransactionRequest') ->
            Serial = T#'TransactionRequest'.transactionId,
            ConnData2 = ConnData#conn_data{serial = Serial},
            prepare_request(ConnData2, T, Rest, AckList, ReqList, Extra);
        {transactionPending, T} when is_record(T, 'TransactionPending') ->
            Serial = T#'TransactionPending'.transactionId,
            ConnData2 = ConnData#conn_data{serial = Serial},
            handle_pending(ConnData2, T, Extra),
            prepare_normal_trans(ConnData2, Rest, AckList, ReqList, Extra);
        {transactionReply, T} when is_tuple(T) andalso  
				   (element(1, T) == 'TransactionReply') ->
	    T2        = transform_transaction_reply_dec(T),
            Serial    = T2#megaco_transaction_reply.transactionId, 
            ConnData2 = ConnData#conn_data{serial = Serial},
            handle_reply(ConnData2, T2, Extra),
	    prepare_normal_trans(ConnData2, Rest, AckList, ReqList, Extra);
        {transactionResponseAck, List} when is_list(List) ->
            prepare_ack(ConnData, List, Rest, AckList, ReqList, Extra);
        {segmentReply, SR} when is_record(SR, 'SegmentReply') ->
	    handle_segment_reply(ConnData, SR, Extra), 
            prepare_normal_trans(ConnData, Rest, AckList, ReqList, Extra)

    end.


prepare_request(ConnData, T, Rest, AckList, ReqList, Extra) ->
    ?rt2("prepare request", [T]),
    ConnHandle = ConnData#conn_data.conn_handle, 
    LocalMid   = ConnHandle#megaco_conn_handle.local_mid,
    TransId    = to_remote_trans_id(ConnData),
    ?rt2("prepare request", [LocalMid, TransId]),
    case lookup_reply(ConnData, TransId) of
        [] ->
	    ?rt3("brand new request"),

            %% Brand new request

	    %% Check pending limit:
	    %% 
	    %% We should actually check the pending limit here
	    %% but since we have to do it later in the 
	    %% handle_request function (just before we call
	    %% the handle_trans_request callback function) we
	    %% can just as well wait (this is after all a very
	    %% unlikely case: see function prepare_trans when 
	    %% monitor_ref == undefined_auto_monitor_ref).
	    %% 

	    #conn_data{send_handle      = SendHandle,
		       pending_timer    = InitTimer,
		       protocol_version = Version,
		       user_mod         = UserMod,
		       user_args        = UserArgs} = ConnData,
	    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
	    M = ?MODULE,
	    F = pending_timeout,
	    A = [ConnHandle, TransId, CurrTimer],     
	    PendingRef = megaco_monitor:apply_after(M, F, A, WaitFor),
            Rep = #reply{send_handle       = SendHandle,
			 trans_id          = TransId,
			 local_mid         = LocalMid,
			 pending_timer_ref = PendingRef,
			 handler           = self(),
			 version           = Version,
                         user_mod          = UserMod,
                         user_args         = UserArgs},
            case megaco_monitor:insert_reply_new(Rep) of
		true ->
		    prepare_normal_trans(ConnData, Rest, AckList, 
					 [{ConnData, TransId, T} | ReqList], 
					 Extra);
		false ->
		    %% Oups - someone got there before we did...
		    ?report_debug(ConnData, 
				  "prepare request: conflicting requests", 
				  [TransId]),
		    send_pending(ConnData),
		    megaco_monitor:cancel_apply_after(PendingRef),
		    prepare_normal_trans(ConnData, Rest, AckList, ReqList, 
					 Extra)
	    end;

	%% We can ignore the Converted value here as we *know*
	%% conn-data to be correct (not faked), so even if 
	%% the record was converted, it will now have correct
	%% values for user_mod and user_args.
        {_Converted, 
	 #reply{state             = State, 
		handler           = Pid,
		pending_timer_ref = Ref} = Rep}
	when (State =:= prepare) orelse (State =:= eval_request) ->

	    ?rt2("request resend", [State, Pid, Ref]),

            %% Pending limit:
	    %% We are still preparing/evaluating the request
            %% Check if the pending limit has been exceeded...
	    %% If the pending limit is _not_ exceeded then
	    %% we shall send a pending (and actually restart 
	    %% the pending timer, but that we cannot do).
	    %% Don't care about Msg and Rep version diff

	    #conn_data{sent_pending_limit = Limit} = ConnData,
	    
	    case check_and_maybe_incr_pending_limit(Limit, sent, TransId) of
		ok ->

		    %% ------------------------------------------
		    %% 
		    %%   Pending limit not exceeded
		    %% 
		    %%   1) Increment number of pendings sent
		    %%      (done in the check function above)
		    %%   2) Send pending message
		    %%      (We should really restart the pending 
		    %%      timer, but we have no way of doing that).
		    %% 
		    %% ------------------------------------------

		    send_pending(ConnData),
		    prepare_normal_trans(ConnData, Rest, AckList, ReqList, 
					 Extra);


		error ->

		    %% -------------------------------------------
		    %% 
		    %%   Pending limit exceeded
		    %% 
		    %%   1) Cancel pending timer
		    %%   2) Send 506 error message to other side
		    %%   3) Inform user (depends on state)
		    %%   4) Set reply in aborted state
		    %% 
		    %% -------------------------------------------

		    %% 
		    %% State == eval_request:
		    %%   This means that the request is currently beeing 
		    %%   evaluated by the user, and the reply timer has 
		    %%   not yet been started. 
		    %%   Either:
		    %%   a) The "other side" will resend (which will 
		    %%      trigger a pending message send) until we pass the 
		    %%      pending limit 
		    %%   b) We will send pending messages (when the pending 
		    %%      timer expire) until we pass the pending limit.
		    %%   In any event, we cannot delete the reply record
		    %%   or the pending counter in this case. Is there
		    %%   a risk we accumulate aborted reply records?
		    %% 
		    %% State == prepare:
		    %%   The user does not know about this request
		    %%   so we can safely perform cleanup.
		    %% 
		    megaco_monitor:cancel_apply_after(Ref),
		    send_pending_limit_error(ConnData),
		    if 
			State == eval_request ->
			    %% 
			    %% What if the user never replies?
			    %% In that case we will have a record
			    %% (and counters) that is never cleaned up...
			    NewFields = 
				[{#reply.state,             aborted}, 
				 {#reply.pending_timer_ref, undefined}], 
			    megaco_monitor:update_reply_fields(TransId, 
							       NewFields),
			    handle_request_abort_callback(ConnData, 
							  TransId, Pid, Extra);
			true ->
			    %% Since the user does not know about
			    %% this call yet, it is safe to cleanup.
			    %% Should we inform?
			    Rep2 = Rep#reply{state = aborted},
			    cancel_reply(ConnData, Rep2, aborted),
			    ok
		    end,
		    prepare_normal_trans(ConnData, Rest, AckList, ReqList, 
					 Extra);


		aborted ->

		    %% -------------------------------------------
		    %% 
		    %%   Pending limit already exceeded
		    %% 
		    %%   Cleanup, just to make sure:
		    %%     reply record & pending counter
		    %% 
		    %% -------------------------------------------

		    Rep2 = Rep#reply{state = aborted},
		    cancel_reply(ConnData, Rep2, aborted),
		    prepare_normal_trans(ConnData, Rest, AckList, ReqList, 
					 Extra)

	    end;

	%% We can ignore the Converted value here as we *know*
	%% conn-data to be correct (not faked), so even if 
	%% the record was converted, it will now have correct
	%% values for user_mod and user_args.
        {_Converted, 
	 #reply{state   = waiting_for_ack, 
		bytes   = Bin, 
		version = Version} = Rep} ->
	    ?rt3("request resend when waiting for ack"),

            %% We have already sent a reply, but the receiver
            %% has obviously not got it. Resend the reply but
            %% don't restart the reply_timer.
            ConnData2 = ConnData#conn_data{protocol_version = Version},
            ?report_trace(ConnData2, 
			  "re-send trans reply", [T | {bytes, Bin}]),
            case megaco_messenger_misc:send_message(ConnData2, true, Bin) of
		{ok, _} ->
		    ok;
		{error, Reason} ->
		    %% Pass it on to the user (via handle_ack)
		    cancel_reply(ConnData2, Rep, Reason)
	    end,
	    prepare_normal_trans(ConnData2, Rest, AckList, ReqList, Extra);


	%% We can ignore the Converted value here as we *know*
	%% conn-data to be correct (not faked), so even if 
	%% the record was converted, it will now have correct
	%% values for user_mod and user_args.
	{_Converted, 
	 #reply{state = aborted} = Rep} ->
	    ?rt3("request resend when already in aborted state"),

	    %% OTP-4956:
	    %% Already aborted so ignore.
	    %% This furthermore means that the abnoxious user at the
	    %% other end has already been informed (pending-limit
	    %% passed => error descriptor sent), but keeps sending...
	    %% 
	    %% Shall we perform a cleanup?
	    cancel_reply(ConnData, Rep, aborted),
	    prepare_normal_trans(ConnData, Rest, AckList, ReqList, Extra)
        end.

prepare_ack(ConnData, [TA | T], Rest, AckList, ReqList, Extra) 
  when is_record(TA, 'TransactionAck') ->
    First     = TA#'TransactionAck'.firstAck,
    Last      = TA#'TransactionAck'.lastAck,
    TA2       = TA#'TransactionAck'{lastAck = asn1_NOVALUE},
    ConnData2 = ConnData#conn_data{serial = First},
    AckList2  = do_prepare_ack(ConnData2, TA2, AckList),
    if
        Last =:= asn1_NOVALUE ->
            prepare_ack(ConnData, T, Rest, AckList2, ReqList, Extra);
        First < Last ->
            TA3 = TA#'TransactionAck'{firstAck = First + 1},
            prepare_ack(ConnData, [TA3 | T], Rest, AckList2, ReqList, Extra);
        First =:= Last ->
            prepare_ack(ConnData, T, Rest, AckList2, ReqList, Extra);
        First > Last ->
            %% Protocol violation from the sender of this ack
            ?report_important(ConnData, "<ERROR> discard trans",
			      [TA, {error, "firstAck > lastAck"}]),
	    prepare_ack(ConnData, T, Rest, AckList2, ReqList, Extra)
    end;
prepare_ack(ConnData, [], Rest, AckList, ReqList, Extra) ->
    prepare_normal_trans(ConnData, Rest, AckList, ReqList, Extra).

do_prepare_ack(ConnData, T, AckList) ->
    TransId = to_remote_trans_id(ConnData),
    case lookup_reply(ConnData, TransId) of
        [] ->
            %% The reply has already been garbage collected. Ignore.
            ?report_trace(ConnData, "discard ack (no receiver)", [T]),
            AckList;
        {_Converted, Rep} when Rep#reply.state =:= waiting_for_ack ->
            %% Don't care about Msg and Rep version diff
            [{ConnData, Rep, T} | AckList];
        {_Converted, _Rep} ->
            %% Protocol violation from the sender of this ack
            ?report_important(ConnData, "<ERROR> discard trans",
			      [T, {error, "got ack before reply was sent"}]),
            AckList
    end.


increment_request_keep_alive_counter(#conn_data{conn_handle = CH}, TransId) ->
    ?rt1(CH, "increment request keep alive counter", [TransId]),
    megaco_config:incr_reply_counter(CH, TransId).

create_or_maybe_increment_request_keep_alive_counter(
  #conn_data{conn_handle = CH}, TransId) ->
    ?rt1(CH, "create or maybe increment request keep alive counter", 
	 [TransId]),
    try
	begin
	    megaco_config:cre_reply_counter(CH, TransId)
	end
    catch 
	_:_ ->
	    megaco_config:incr_reply_counter(CH, TransId)
    end.
	

check_and_maybe_create_pending_limit(infinity, _, _) ->
    ok;
check_and_maybe_create_pending_limit(Limit, Direction, TransId) ->
    ?rt2("check and maybe create pending limit counter", 
	 [Limit, Direction, TransId]),
    try megaco_config:get_pending_counter(Direction, TransId) of
	Val when Val =< Limit ->
	    %% Since we have no intention to increment here, it
	    %% is ok to be _at_ the limit
	    ok;
	_ ->
	    aborted
    catch 
	_:_ ->
	    %% Has not been created yet (connect).
	    megaco_config:cre_pending_counter(Direction, TransId, 0),
	    ok
    end.

%% check_and_maybe_create_pending_limit(infinity, _, _) ->
%%     ok;
%% check_and_maybe_create_pending_limit(Limit, Direction, TransId) ->
%%     case (catch megaco_config:get_pending_counter(Direction, TransId)) of
%% 	{'EXIT', _} ->
%% 	    %% Has not been created yet (connect).
%% 	    megaco_config:cre_pending_counter(Direction, TransId, 0),
%% 	    ok;
%% 	Val when Val =< Limit ->
%% 	    %% Since we have no intention to increment here, it
%% 	    %% is ok to be _at_ the limit
%% 	    ok;
%% 	_ ->
%% 	    aborted
%%     end.


check_pending_limit(infinity, _, _) ->
    {ok, 0};
check_pending_limit(Limit, Direction, TransId) ->
    ?rt2("check pending limit", [Direction, Limit, TransId]),
    try megaco_config:get_pending_counter(Direction, TransId) of
	Val when Val =< Limit ->
	    %% Since we have no intention to increment here, it
	    %% is ok to be _at_ the limit
	    ?rt2("check pending limit - ok", [Val]),
	    {ok, Val};
	_Val ->
	    ?rt2("check pending limit - aborted", [_Val]),
	    aborted
    catch 
	_:_ ->
	    %% This function is only called when we "know" the 
	    %% counter to exist. So, the only reason that this 
	    %% would happen is of the counter has been removed.
	    %% This only happen if the pending limit has been 
	    %% reached. In any case, this is basically the same 
	    %% as aborted!
	    ?rt2("check pending limit - exit", []),
	    aborted
    end.


check_and_maybe_incr_pending_limit(infinity, _, _) ->
    ok;
check_and_maybe_incr_pending_limit(Limit, Direction, TransId) ->
    %% 
    %% We need this kind of test to detect when we _pass_ the limit
    %% 
    ?rt2("check and maybe incr pending limit", [{direction,      Direction}, 
						{transaction_id, TransId}, 
						{counter_limit,  Limit}]),
    try megaco_config:get_pending_counter(Direction, TransId) of
	Val when Val > Limit ->
	    ?rt2("check and maybe incr - aborted", [{counter_value, Val}]),
	    aborted;      % Already passed the limit
	Val ->
	    ?rt2("check and maybe incr - incr", [{counter_value, Val}]),
	    megaco_config:incr_pending_counter(Direction, TransId),
	    if 
		Val < Limit ->
		    ok;   % Still within the limit
		true ->
		    ?rt2("check and maybe incr - error", 
			 [{counter_value, Val}]),
		    error % Passed the limit
	    end
    catch 
	_:_ ->
	    %% Has not been created yet (connect).
	    %% Try create it, but bevare of possible raise condition
	    try
		begin
		    megaco_config:cre_pending_counter(Direction, TransId, 1),
		    ok
		end
	    catch
		_:_ ->
		    %% Ouch, raise condition, increment instead...
		    megaco_config:incr_pending_counter(Direction, TransId),
		    ok
	    end
    end.


%% BUGBUG BUGBUG BUGBUG
%% 
%% Do we know that the Rep is still valid? A previous transaction
%% could have taken a lot of time.
%% 
handle_request({ConnData, TransId, T}, Extra) ->
    case handle_request(ConnData, TransId, T, Extra) of
	{pending, _RequestData} ->
	    handle_long_request(ConnData, TransId, T, Extra);
	Else ->
	    Else
    end.

handle_request(ConnData, TransId, T, Extra) ->
    ?report_trace(ConnData, "handle request", [TransId, T]),

    %% Pending limit:
    %% Ok, before we begin, lets check that this request 
    %% has not been aborted. I.e. exceeded the pending 
    %% limit, so go check it...

    #conn_data{sent_pending_limit = Limit} = ConnData,

    case check_and_maybe_create_pending_limit(Limit, sent, TransId) of
	ok ->
	    %% Ok so far, now update state
	    case megaco_monitor:update_reply_field(TransId, 
						   #reply.state, 
						   eval_request) of
		true ->
		    Actions = T#'TransactionRequest'.actions,
		    {AckAction, SendReply} = 
			handle_request_callback(ConnData, TransId, Actions, 
						T, Extra),

		    %% Next step, while we where in the callback function,
		    %% the pending limit could have been exceeded, so check
		    %% it again...
		    do_handle_request(AckAction, SendReply, 
				      ConnData, TransId);

		false ->
		    %% Ugh?
		    ignore
	    end;

	aborted ->
	    %% Pending limit
	    %% Already exceeded the limit
	    %% The user does not yet know about this request, so
	    %% don't bother telling that it has been aborted...
	    %% Furthermore, the reply timer has not been started,
	    %% so do the cleanup now
	    ?rt1(ConnData, "pending limit already passed", [TransId]),
	    case lookup_reply(ConnData, TransId) of
		{_Converted, Rep} ->
		    cancel_reply(ConnData, Rep, aborted);
		_ ->
		    ok
	    end,
	    ignore
    end.

do_handle_request(_, ignore, _ConnData, _TransId) ->
    ?rt1(_ConnData, "ignore: don't reply", [_TransId]),
    ignore;
do_handle_request(_, ignore_trans_request, ConnData, TransId) ->
    ?rt1(ConnData, "ignore trans request: don't reply", [TransId]),
    case lookup_reply(ConnData, TransId) of
	{_Converted, #reply{} = Rep} ->
	    cancel_reply(ConnData, Rep, ignore);
	_ ->
	    ignore
    end;
do_handle_request({pending, _RequestData}, {aborted, ignore}, _, _) ->
    ?rt2("handle request: pending - aborted - ignore => don't reply", []),
    ignore;
do_handle_request({pending, _RequestData}, {aborted, _SendReply}, _, _) ->
    ?rt2("handle request: pending - aborted => don't reply", []),
    ignore;
do_handle_request({pending, RequestData}, _SendReply, _ConnData, _) ->
    ?rt2("handle request: pending", [RequestData]),
    {pending, RequestData};
do_handle_request(AckAction, {ok, Bin}, ConnData, TransId) 
  when is_binary(Bin) ->
    ?rt1(ConnData, "handle request - ok", [AckAction, TransId]),
    case lookup_reply(ConnData, TransId) of
	{_Converted, #reply{pending_timer_ref = PendingRef} = Rep} ->

	    #conn_data{reply_timer = InitTimer,
		       conn_handle = ConnHandle} = ConnData,

	    %% Pending limit update:
	    %%   - Cancel the pending timer, if running
	    %%   - Delete the pending counter
	    %% 

	    megaco_monitor:cancel_apply_after(PendingRef),
	    megaco_config:del_pending_counter(sent, TransId),

	    Method = timer_method(AckAction),
	    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
	    OptBin = opt_garb_binary(CurrTimer, Bin),
	    M = ?MODULE,
	    F = reply_timeout,
	    A = [ConnHandle, TransId, CurrTimer],
	    Ref = megaco_monitor:apply_after(Method, M, F, A, 
					     WaitFor),
	    Rep2 = Rep#reply{pending_timer_ref = undefined,
			     handler           = undefined,
			     bytes             = OptBin,
			     state             = waiting_for_ack,
			     timer_ref         = Ref,
			     ack_action        = AckAction},
	    megaco_monitor:insert_reply(Rep2), % Timing problem?
	    ignore;

	_ ->
	    %% Been removed already?
	    ignore
    end;
do_handle_request(AckAction, {ok, {Sent, NotSent}}, ConnData, TransId) 
  when is_list(Sent) andalso is_list(NotSent) ->
    ?rt1(ConnData, "handle request - ok [segmented reply]", 
	 [AckAction, TransId]),

    case lookup_reply(ConnData, TransId) of
	{_Converted, #reply{pending_timer_ref = PendingRef} = Rep} ->

	    %% d("do_handle_request -> found reply record:"
	    %%   "~n   Rep: ~p", [Rep]),
	    
	    #conn_data{reply_timer = InitTimer,
		       conn_handle = ConnHandle} = ConnData,

	    %% Pending limit update:
	    %%   - Cancel the pending timer, if running
	    %%   - Delete the pending counter
	    %% 

	    megaco_monitor:cancel_apply_after(PendingRef),
	    megaco_config:del_pending_counter(sent, TransId),

	    Method = timer_method(AckAction),
	    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
	    Garb = fun(Bin) -> opt_garb_binary(CurrTimer, Bin) end,
	    OptBins = [{SN, Garb(Bin), undefined} || {SN, Bin} <- Sent],

	    M   = ?MODULE,
	    F   = reply_timeout,
	    A   = [ConnHandle, TransId, CurrTimer],
	    Ref = megaco_monitor:apply_after(Method, M, F, A, WaitFor),

	    Rep2 = Rep#reply{pending_timer_ref = undefined,
			     handler           = undefined,
			     bytes             = OptBins,
			     state             = waiting_for_ack,
			     timer_ref         = Ref,
			     ack_action        = AckAction,
			     segments          = NotSent},
	    megaco_monitor:insert_reply(Rep2), % Timing problem?

	    ignore;
	_ ->
	    %% Been removed already?
	    ignore
    end;
do_handle_request(_, {error, aborted}, ConnData, TransId) ->
    ?report_trace(ConnData, "aborted during our absence", [TransId]),
    case lookup_reply(ConnData, TransId) of
	{_Converted, Rep} ->
	    cancel_reply(ConnData, Rep, aborted);
	_ ->
	    ok
    end,
    ignore;
do_handle_request(AckAction, {error, Reason}, ConnData, TransId) ->
    ?report_trace(ConnData, "error", [TransId, Reason]),
    case lookup_reply(ConnData, TransId) of
	{_Converted, Rep} ->
	    Rep2 = Rep#reply{state      = waiting_for_ack,
			     ack_action = AckAction},
	    cancel_reply(ConnData, Rep2, Reason);
	_ ->
	    ok
    end,
    ignore;
do_handle_request(AckAction, SendReply, ConnData, TransId) ->
    ?report_trace(ConnData, "unknown send trans reply result", 
		  [TransId, AckAction, SendReply]),
    ignore.
    

handle_requests([{ConnData, TransId, T} | Rest], Pending, Extra) ->
    ?rt2("handle requests", [TransId]),
    case handle_request(ConnData, TransId, T, Extra) of
	{pending, RequestData} ->
	    handle_requests(Rest, [{ConnData,TransId,RequestData} | Pending], Extra);
	_ ->
	    handle_requests(Rest, Pending, Extra)
    end;
handle_requests([], Pending, _Extra) ->
    ?rt2("handle requests - done", [Pending]),
    Pending.

%% opt_garb_binary(timeout, _Bin) -> garb_binary; % Need msg at restart of timer
opt_garb_binary(_Timer,   Bin) -> Bin.

timer_method(discard_ack) ->
    apply_method;
timer_method(_) ->
    spawn_method.


handle_long_request({ConnData, TransId, RequestData}, Extra) ->

    ?rt2("handle long request", [TransId, RequestData]),

    %% Pending limit:
    %% We need to check the pending limit, in case it was
    %% exceeded before we got this far...
    %% We dont need to be able to create the counter here,
    %% since that was done in the handle_request function.

    #conn_data{sent_pending_limit = Limit} = ConnData,

    case check_pending_limit(Limit, sent, TransId) of
	{ok, _} ->
	    handle_long_request(ConnData, TransId, RequestData, Extra);
	_ ->
	    %% Already exceeded the limit
	    ignore
    end.

handle_long_request(ConnData, TransId, RequestData, Extra) ->
    ?report_trace(ConnData, "callback: trans long request",
		  [TransId, {request_data, RequestData}]),
    
    %% Attempt to update the handler field for this reply record
    %% (if there is one).
    case megaco_monitor:update_reply_field(TransId, #reply.handler, self()) of
	true ->
	    {AckAction, Res} = 
		handle_long_request_callback(ConnData, TransId, 
					     RequestData, Extra),
	    do_handle_long_request(AckAction, Res, ConnData, TransId);
	 false ->
	    %% Been removed already?
	    ignore
    end.


do_handle_long_request(AckAction, {ok, Bin}, ConnData, TransId) ->
    case megaco_monitor:lookup_reply_field(TransId, #reply.trans_id) of
	{ok, _} ->
	    Method = timer_method(AckAction),
	    InitTimer = ConnData#conn_data.reply_timer,
	    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
	    OptBin = opt_garb_binary(CurrTimer, Bin),
	    ConnHandle = ConnData#conn_data.conn_handle,
	    M = ?MODULE,
	    F = reply_timeout,
	    A = [ConnHandle, TransId, CurrTimer],
	    Ref = megaco_monitor:apply_after(Method, M, F, A, WaitFor),
	    NewFields = 
		[{#reply.bytes,      OptBin},
		 {#reply.state,      waiting_for_ack},
		 {#reply.timer_ref,  Ref},
		 {#reply.ack_action, AckAction}],
	    megaco_monitor:update_reply_fields(TransId, NewFields); % Timing problem?
	_ ->
	    %% Been removed already?
	    ignore
    end;
do_handle_long_request(_, {error, Reason}, ConnData, TransId) ->
    ?report_trace(ConnData, "send trans reply", [TransId, {error, Reason}]),
    ignore.
    
handle_request_abort_callback(ConnData, TransId, Pid) ->
    Extra = ?default_user_callback_extra, 
    handle_request_abort_callback(ConnData, TransId, Pid, Extra).

handle_request_abort_callback(ConnData, TransId, Pid, Extra) ->
    ?report_trace(ConnData, "callback: trans request aborted", [TransId, Pid]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Serial     = TransId#trans_id.serial,
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, Serial, Pid | UserArgs];
	    _ ->
		[ConnHandle, Version, Serial, Pid, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_trans_request_abort, Args)),
    ?report_debug(ConnData, "return: trans request aborted", 
		  [TransId, {return, Res}]),
    case Res of
	ok ->
	    ok;
	_ ->
	    warning_msg("transaction request abort callback failed: ~w", 
			[Res]),
	    ok
    end.

handle_request_callback(ConnData, TransId, Actions, T, Extra) ->
    ?report_trace(ConnData, "callback: trans request", [T]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, Actions | UserArgs];
	    _ ->
		[ConnHandle, Version, Actions, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_trans_request, Args)),
    ?report_debug(ConnData, "return: trans request", [T, {return, Res}]),
    case Res of
	ignore ->  %% NOTE: Only used for testing!!
	    {discard_ack, ignore};

	ignore_trans_request -> 
	    {discard_ack, ignore_trans_request};

	{discard_ack, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {discard_ack, SendReply};
	{discard_ack, Error} when is_record(Error, 'ErrorDescriptor') ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {discard_ack, SendReply};
	{discard_ack, Replies, SendOpts} when is_list(Replies) andalso 
					      is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, asn1_NOVALUE),
	    {discard_ack, SendReply};
	{discard_ack, Error, SendOpts} 
	when is_record(Error, 'ErrorDescriptor') andalso 
	     is_list(SendOpts) ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, asn1_NOVALUE),
	    {discard_ack, SendReply};

	{{handle_pending_ack, AckData}, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], when_pending_sent),
	    {{handle_ack, AckData}, SendReply};
	{{handle_pending_ack, AckData}, Error} 
	when is_record(Error, 'ErrorDescriptor') ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], when_pending_sent),
	    {{handle_ack, AckData}, SendReply};
	{{handle_pending_ack, AckData}, Replies, SendOpts} 
	when is_list(Replies) andalso 
	     is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, when_pending_sent),
	    {{handle_ack, AckData}, SendReply};
	{{handle_pending_ack, AckData}, Error, SendOpts} 
	when is_record(Error, 'ErrorDescriptor') andalso 
	     is_list(SendOpts) ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, when_pending_sent),
	    {{handle_ack, AckData}, SendReply};

	{{handle_ack, AckData}, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], 'NULL'),
	    {{handle_ack, AckData}, SendReply};
	{{handle_ack, AckData}, Error} 
	when is_record(Error, 'ErrorDescriptor') ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], 'NULL'),
	    {{handle_ack, AckData}, SendReply};
	{{handle_ack, AckData}, Replies, SendOpts} 
	when is_list(Replies) andalso 
	     is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, 'NULL'),
	    {{handle_ack, AckData}, SendReply};
	{{handle_ack, AckData}, Error, SendOpts} 
	when is_record(Error, 'ErrorDescriptor') andalso 
	     is_list(SendOpts) ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, 'NULL'),
	    {{handle_ack, AckData}, SendReply};

	{{handle_sloppy_ack, AckData}, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {{handle_ack, AckData}, SendReply};
	{{handle_sloppy_ack, AckData}, Error} 
	when is_record(Error, 'ErrorDescriptor') ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {{handle_ack, AckData}, SendReply};
	{{handle_sloppy_ack, AckData}, Replies, SendOpts} 
	when is_list(Replies) andalso 
	     is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, asn1_NOVALUE),
	    {{handle_ack, AckData}, SendReply};
	{{handle_sloppy_ack, AckData}, Error, SendOpts} 
	when is_record(Error, 'ErrorDescriptor') andalso 
	     is_list(SendOpts) ->
	    Reply     = {transactionError, Error},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, asn1_NOVALUE),
	    {{handle_ack, AckData}, SendReply};
	
	{pending, RequestData} ->
	    %% The user thinks that this request will take
	    %% quite a while to evaluate. Maybe respond with 
	    %% a pending trans (depends on the pending limit)
	    SendReply = maybe_send_pending(ConnData, TransId),
	    {{pending, RequestData}, SendReply};
	
	Error ->
	    ErrorText = atom_to_list(UserMod),
	    ED = #'ErrorDescriptor'{
	      errorCode = ?megaco_internal_gateway_error,
	      errorText = ErrorText},
	    ?report_important(ConnData, 
			      "callback: <ERROR> trans request",
			      [ED, {error, Error}]),
	    error_msg("transaction request callback failed: ~w", [Error]),
	    Reply = {transactionError, ED},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {discard_ack, SendReply}
    end.

handle_long_request_callback(ConnData, TransId, RequestData, Extra) ->
    ?report_trace(ConnData, "callback: trans long request", [RequestData]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, RequestData | UserArgs];
	    _ ->
		[ConnHandle, Version, RequestData, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_trans_long_request, Args)),
    ?report_debug(ConnData, "return: trans long request",
		  [{request_data, RequestData}, {return, Res}]),
    case Res of
	ignore ->
	    {discard_ack, ignore};
	
	{discard_ack, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {discard_ack, SendReply};
	{discard_ack, Replies, SendOpts} when is_list(Replies) andalso 
					      is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, asn1_NOVALUE),
	    {discard_ack, SendReply};
	
	{{handle_ack, AckData}, Replies} when is_list(Replies) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], 'NULL'),
	    {{handle_ack, AckData}, SendReply};
	{{handle_ack, AckData}, Replies, SendOpts} when is_list(Replies) 
							andalso 
							is_list(SendOpts) ->
	    Reply     = {actionReplies, Replies},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 SendOpts, 'NULL'),
	    {{handle_ack, AckData}, SendReply};
	
	Error ->
	    ErrorText = atom_to_list(UserMod),
	    ED = #'ErrorDescriptor'{errorCode = ?megaco_internal_gateway_error,
				    errorText = ErrorText},
	    ?report_important(ConnData, "callback: <ERROR> trans long request",
			      [ED, {error, Error}]),
	    error_msg("long transaction request callback failed: ~w", [Error]),
	    Reply     = {transactionError, ED},
	    SendReply = maybe_send_reply(ConnData, TransId, Reply, 
					 [], asn1_NOVALUE),
	    {discard_ack, SendReply}
    end.

handle_pending(ConnData, T, Extra) ->
    TransId = to_local_trans_id(ConnData),
    ?rt2("handle pending", [T, TransId]),
    case megaco_monitor:lookup_request(TransId) of
	[Req] ->

	    %% ------------------------------------------
	    %% 
	    %%   Check received pending limit
	    %% 
	    %% ------------------------------------------

	    Limit = ConnData#conn_data.recv_pending_limit,
	    case check_and_maybe_incr_pending_limit(Limit, 
						    recv, TransId) of
		
		ok ->
		    %% ----------------------------------------------------
		    %% 
		    %%      Received pending limit not exceeded     
		    %% 
		    %% ----------------------------------------------------

		    handle_recv_pending(ConnData, TransId, Req, T);
		
		error ->
		    %% ----------------------------------------------------
		    %% 
		    %%      Received pending limit exceeded     
		    %% 
		    %%      Time to give up on this transaction
		    %%      1) Delete request record
		    %%      2) Cancel timers
		    %%      3) Delete the (receive) pending counter
		    %%      4) Inform the user (handle_trans_reply)
		    %% 
		    %% ----------------------------------------------------

		    handle_recv_pending_error(ConnData, TransId, Req, T, Extra);

		
		aborted ->
		    %% ----------------------------------------------------
		    %% 
		    %%      Received pending limit already exceeded     
		    %% 
		    %% BMK BMK BMK -- can this really happen?
		    %% 
		    %%      The user has already been notified about this
		    %%      (see error above)
		    %% 
		    %% ----------------------------------------------------

		    ok

	    end;

	[] ->
	    ?report_trace(ConnData, "remote pending (no receiver)", [T]),
	    return_unexpected_trans(ConnData, T, Extra)
    end.

handle_recv_pending(#conn_data{long_request_resend = LRR,
			       conn_handle         = ConnHandle} = ConnData, 
		    TransId,
		    #request{timer_ref       = {short, Ref},
			     init_long_timer = InitTimer}, T) ->

    ?rt2("handle pending - long request", [LRR, InitTimer]),

    %% The request seems to take a while,
    %% let's reset our transmission timer.
    %% We now know the other side has got
    %% the request and is working on it,
    %% so there is no need to keep the binary
    %% message for re-transmission.
    
    %% Start using the long timer. 
    %% We can now drop the "bytes", since we will
    %% not resend from now on.
    
    megaco_monitor:cancel_apply_after(Ref),
    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
    ConnHandle = ConnData#conn_data.conn_handle,
    M = ?MODULE,
    F = request_timeout,
    A = [ConnHandle, TransId],
    Ref2 = megaco_monitor:apply_after(M, F, A, WaitFor),
    NewFields = 
	case LRR of
	    true ->
		[{#request.timer_ref,  {long, Ref2}},
		 {#request.curr_timer, CurrTimer}];
	    false ->
		[{#request.bytes,      {no_send, garb_binary}}, 
		 {#request.timer_ref,  {long, Ref2}},
		 {#request.curr_timer, CurrTimer}]
	end,
    ?report_trace(ConnData, "trans pending (timer restarted)", [T]),
    megaco_monitor:update_request_fields(TransId, NewFields); % Timing problem?
    
handle_recv_pending(_ConnData, _TransId,
		    #request{timer_ref  = {long, _Ref},
			     curr_timer = timeout}, _T) ->

    ?rt3("handle pending - timeout"),

    %% The request seems to take a while,
    %% let's reset our transmission timer.
    %% We now know the other side has got
    %% the request and is working on it,
    %% so there is no need to keep the binary
    %% message for re-transmission.
    
    %% This can happen if the timer is running for the last 
    %% time. I.e. next time it expires, will be the last.
    %% Therefor we really do not need to do anything here.
    %% The cleanup will be done in request_timeout.
    
    ok;

handle_recv_pending(#conn_data{conn_handle = ConnHandle} = ConnData, TransId,
		    #request{timer_ref  = {long, Ref},
			     curr_timer = CurrTimer}, T) ->
    
    ?rt2("handle pending - still waiting", [CurrTimer]),

    %% The request seems to take a while,
    %% let's reset our transmission timer.
    %% We now know the other side has got
    %% the request and is working on it,
    %% so there is no need to keep the binary
    %% message for re-transmission.
    
    %% We just need to recalculate the timer, i.e. 
    %% increment the timer (one "slot" has been consumed).
    
    megaco_monitor:cancel_apply_after(Ref),
    {WaitFor, Timer2} = megaco_timer:restart(CurrTimer),
    ConnHandle = ConnData#conn_data.conn_handle,
    M = ?MODULE,
    F = request_timeout,
    A = [ConnHandle, TransId],
    Ref2 = megaco_monitor:apply_after(M, F, A, WaitFor),
    NewFields = 
	[{#request.timer_ref,  {long, Ref2}},
	 {#request.curr_timer, Timer2}],
    ?report_trace(ConnData, 
		  "long trans pending"
		  " (timer restarted)", [T]),
    %% Timing problem?
    megaco_monitor:update_request_fields(TransId, NewFields).
    

handle_recv_pending_error(ConnData, TransId, Req, T, Extra) ->
    %% 1) Delete the request record
    megaco_monitor:delete_request(TransId),

    %% 2) Possibly cancel the timer
    case Req#request.timer_ref of
	{_, Ref} ->
	    megaco_monitor:cancel_apply_after(Ref);
	_ ->
	    ok
    end,
    
    %% 3) Delete the (receive) pending counter
    megaco_config:del_pending_counter(recv, TransId),
    
    %% 4) Inform the user that his/her request reached 
    %%    the receive pending limit
    UserMod   = Req#request.user_mod,
    UserArgs  = Req#request.user_args,
    Action    = Req#request.reply_action,
    UserData  = Req#request.reply_data,
    UserReply = {error, exceeded_recv_pending_limit},
    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
				   user_args    = UserArgs,
				   reply_action = Action,
				   reply_data   = UserData},

    ?report_trace(ConnData, "receive pending limit reached", [T]),
    return_reply(ConnData2, TransId, UserReply, Extra).


%% 
%% This _is_ a segmented message.
%% 
%% Since this is not the last segment, we shall not send any ack.
%% (even if three-way-handshake has been configured).
%% 
handle_reply(
  ConnData, 
  #megaco_transaction_reply{segmentNumber        = SN,
			    segmentationComplete = asn1_NOVALUE} = T, Extra) 
  when is_integer(SN) ->
    TransId = to_local_trans_id(ConnData),
    ?rt2("handle segmented reply", [T, TransId, SN]),
    case megaco_monitor:lookup_request(TransId) of

	%% ---------------------------------------------------------
	%% The first segment, so stop the request timer. No longer
	%% needed when the segment(s) start to arrive.

	[#request{timer_ref     = {_Type, Ref},
		  seg_recv      = [],
		  seg_timer_ref = undefined} = Req] -> 

            %% Don't care about Req and Rep version diff
            ?report_trace(ConnData, "[segmented] trans reply - first seg", 
			  [T]),

	    %% Stop the request timer
            megaco_monitor:cancel_apply_after(Ref), %% OTP-4843

	    %% Acknowledge the segment
	    send_segment_reply(ConnData, SN),

	    %% First segment for this reply
	    NewFields = 
		[{#request.timer_ref, undefined},
		 {#request.seg_recv,  [SN]}],
	    megaco_monitor:update_request_fields(TransId, NewFields),
	    
	    %% Handle the reply
	    UserMod   = Req#request.user_mod,
	    UserArgs  = Req#request.user_args,
	    Action    = Req#request.reply_action,
	    UserData  = Req#request.reply_data,
	    UserReply =
		case T#megaco_transaction_reply.transactionResult of
		    {transactionError, Reason} ->
			{error, {SN, false, Reason}};
		    {actionReplies, Replies} ->
			{ok, {SN, false, Replies}}
		end,
	    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
					   user_args    = UserArgs,
					   reply_action = Action,
					   reply_data   = UserData},
	    return_reply(ConnData2, TransId, UserReply, Extra);


	%% ---------------------------------------------------------
	%% This is not the first segment.
	%% The segment timer has not been started, so the last
	%% segment have been received.
	%% We must check that this is not a re-transmission!

	[#request{seg_recv      = Segs,
		  seg_timer_ref = undefined} = Req] -> 
            %% Don't care about Req and Rep version diff
            ?report_trace(ConnData, "[segmented] trans reply - no seg acc", 
			  [T]),

	    %% Acknowledge the segment
	    send_segment_reply(ConnData, SN),

	    %% Updated/handle received segment
	    case lists:member(SN, Segs) of
		true ->
		    %% This is a re-transmission, so we shall not pass
		    %% it on to the user (or update the request record).
		    ok;
		false ->
		    %% First time for this segment
		    megaco_monitor:update_request_field(TransId,
							#request.seg_recv,
							[ SN | Segs ]),

		    %% Handle the reply
		    UserMod   = Req#request.user_mod,
		    UserArgs  = Req#request.user_args,
		    Action    = Req#request.reply_action,
		    UserData  = Req#request.reply_data,
		    UserReply =
			case T#megaco_transaction_reply.transactionResult of
			    {transactionError, Reason} ->
				{error, {SN, false, Reason}};
			    {actionReplies, Replies} ->
				{ok, {SN, false, Replies}}
			end,
		    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
						   user_args    = UserArgs,
						   reply_action = Action,
						   reply_data   = UserData},
		    return_reply(ConnData2, TransId, UserReply, Extra)
		
		end;


	%% ---------------------------------------------------------
	%% The segment timer is running! 
	%% This could be the last (out-of-order) segment!
	%% We must check that this is not a re-transmission!

	[#request{seg_recv      = Segs,
		  seg_timer_ref = SegRef} = Req] -> 
            %% Don't care about Req and Rep version diff
            ?report_trace(ConnData, "[segmented] trans reply - no seg acc", 
			  [T]),

	    %% Acknowledge the segment
	    send_segment_reply(ConnData, SN),

	    %% Updated received segments
	    case lists:member(SN, Segs) of 
		true ->
		    %% This is a re-transmission
		    ok;
		false ->
		    %% First time for this segment, 
		    %% we may now have a complete set
		    Last = 
			case is_all_segments([SN | Segs]) of
			    {true, _Sorted} ->
				megaco_monitor:cancel_apply_after(SegRef),
				megaco_monitor:delete_request(TransId),
				send_ack(ConnData),
				true;
			    {false, Sorted} ->
				megaco_monitor:update_request_field(TransId,
								    #request.seg_recv,
								    Sorted),
				false
			end,

		    %% Handle the reply
		    UserMod   = Req#request.user_mod,
		    UserArgs  = Req#request.user_args,
		    Action    = Req#request.reply_action,
		    UserData  = Req#request.reply_data,
		    UserReply =
			case T#megaco_transaction_reply.transactionResult of
			    {transactionError, Reason} ->
				{error, {SN, Last, Reason}};
			    {actionReplies, Replies} ->
				{ok, {SN, Last, Replies}}
			end,
		    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
						   user_args    = UserArgs,
						   reply_action = Action,
						   reply_data   = UserData},
		    return_reply(ConnData2, TransId, UserReply, Extra)
		
	    end;
	    

	[] ->
	    ?report_trace(ConnData, "trans reply (no receiver)", [T]),
	    return_unexpected_trans(ConnData, T, Extra)
    end;


%% 
%% This _is_ a segmented message and it's the last segment of the
%% message.
%% 
handle_reply(
  ConnData, 
  #megaco_transaction_reply{segmentNumber        = SN,
			    segmentationComplete = 'NULL'} = T, Extra) 
  when is_integer(SN) ->
    TransId = to_local_trans_id(ConnData),
    ?rt2("handle (last) segmented reply", [T, TransId, SN]),
    case megaco_monitor:lookup_request(TransId) of

	%% ---------------------------------------------------------
	%% The first segment, so stop the request timer. No longer
	%% needed when the segment(s) start to arrive.

	[#request{timer_ref     = {_Type, Ref},
		  seg_recv      = [],
		  seg_timer_ref = undefined} = Req] -> 

            %% Don't care about Req and Rep version diff
            ?report_trace(ConnData, "[segmented] trans reply - "
			  "first/complete seg", [T]),

	    %% Stop the request timer
            megaco_monitor:cancel_apply_after(Ref), %% OTP-4843

	    %% Acknowledge the ("last") segment
	    send_segment_reply_complete(ConnData, SN),

	    %% It is ofcourse pointless to split
	    %% a transaction into just one segment,
	    %% but just to be sure, we handle that 
	    %% case also 
	    Last = 
		if 
		    SN > 1 ->
			%% More then one segment
			%% First time for this segment
			ConnHandle = ConnData#conn_data.conn_handle,
			InitSegTmr = Req#request.init_seg_timer,
			{WaitFor, CurrTimer} = megaco_timer:init(InitSegTmr),
			M = ?MODULE,
			F = segment_timeout,
			A = [ConnHandle, TransId, CurrTimer],
			SegRef = 
			    megaco_monitor:apply_after(M, F, A, WaitFor),
			NewFields = 
			    [{#request.timer_ref,     undefined},
			     {#request.seg_recv,      [SN]},
			     {#request.seg_timer_ref, SegRef}],
			megaco_monitor:update_request_fields(TransId, NewFields),
			false;
		    true ->
			%% Just one segment!
			megaco_monitor:delete_request(TransId),
			send_ack(ConnData),
			true
		end,
	    
	    %% Handle the reply
	    UserMod   = Req#request.user_mod,
	    UserArgs  = Req#request.user_args,
	    Action    = Req#request.reply_action,
	    UserData  = Req#request.reply_data,
	    UserReply =
		case T#megaco_transaction_reply.transactionResult of
		    {transactionError, Reason} ->
			{error, {SN, Last, Reason}};
		    {actionReplies, Replies} ->
			{ok, {SN, Last, Replies}}
		end,
	    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
					   user_args    = UserArgs,
					   reply_action = Action,
					   reply_data   = UserData},
	    return_reply(ConnData2, TransId, UserReply, Extra);


	[#request{seg_recv  = Segs} = Req] -> 
            %% Don't care about Req and Rep version diff
            ?report_trace(ConnData, "[segmented] trans reply - no seg acc", 
			  [T]),

	    %% Acknowledge the ("last") segment
	    send_segment_reply_complete(ConnData, SN),

	    %% Updated received segments
	    %% This is _probably_ the last segment, but some of
	    %% the previous segments may have been lost, so we 
	    %% may not have a complete set!
	    case lists:member(SN, Segs) of 
		true ->
		    %% This is a re-transmission
		    ok;
		false ->
		    Last = 
			case is_all_segments([SN | Segs]) of
			    {true, _Sorted} ->
				?report_trace(ConnData, 
					      "[segmented] trans reply - "
					      "complete set", [T]),
				megaco_monitor:delete_request(TransId),
				send_ack(ConnData),
				true;
			    {false, Sorted} ->
				ConnHandle = ConnData#conn_data.conn_handle,
				InitSegTmr = Req#request.init_seg_timer,
				{WaitFor, CurrTimer} = 
				    megaco_timer:init(InitSegTmr),
				M = ?MODULE,
				F = segment_timeout,
				A = [ConnHandle, TransId, CurrTimer],
				SegRef = 
				    megaco_monitor:apply_after(M, F, A, 
							       WaitFor),
				NewFields = 
				    [{#request.seg_recv,      Sorted},
				     {#request.seg_timer_ref, SegRef}],
				megaco_monitor:update_request_fields(TransId, NewFields),
				false
			end,
		    
		    %% Handle the reply
		    UserMod   = Req#request.user_mod,
		    UserArgs  = Req#request.user_args,
		    Action    = Req#request.reply_action,
		    UserData  = Req#request.reply_data,
		    UserReply =
			case T#megaco_transaction_reply.transactionResult of
			    {transactionError, Reason} ->
				{error, {SN, Last, Reason}};
			    {actionReplies, Replies} ->
				{ok, {SN, Last, Replies}}
			end,
		    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
						   user_args    = UserArgs,
						   reply_action = Action,
						   reply_data   = UserData},
		    return_reply(ConnData2, TransId, UserReply, Extra)

	    end;

	[] ->
	    ?report_trace(ConnData, "trans reply (no receiver)", [T]),
	    return_unexpected_trans(ConnData, T, Extra)
    end;


%% 
%% This is _not_ a segmented message, 
%% i.e. it's an ordinary transaction reply
%% 
handle_reply(#conn_data{conn_handle = CH} = CD, T, Extra) ->
    TransId = to_local_trans_id(CD),
    ?rt2("handle reply", [T, TransId]),
    case {megaco_monitor:request_lockcnt_inc(TransId),
	  megaco_monitor:lookup_request(TransId)} of
	{_Cnt, [Req]} when (is_record(Req, request) andalso 
			    (CD#conn_data.cancel =:= true)) -> 
	    ?TC_AWAIT_REPLY_EVENT(true),
	    ?report_trace(CD, "trans reply - cancel(1)", [T]),
	    do_handle_reply_cancel(CD, Req, T);

	{Cnt, [#request{remote_mid = RMid} = Req]} when 
	((Cnt =:= 1) andalso 
	 ((RMid =:= preliminary_mid) orelse 
	  (RMid =:= CH#megaco_conn_handle.remote_mid))) -> 
	    ?TC_AWAIT_REPLY_EVENT(false),
	    %% Just in case conn_data got update after our lookup
	    %% but before we looked up the request record, we
	    %% check the cancel field again.
	    case megaco_config:conn_info(CD, cancel) of
		true ->
		    ?report_trace(CD, "trans reply - cancel(2)", [T]),
		    megaco_monitor:request_lockcnt_del(TransId), 
		    do_handle_reply_cancel(CD, Req, T);
		false ->
		    ?report_trace(CD, "trans reply", [T]),
		    do_handle_reply(CD, Req, TransId, T, Extra)
	    end;

	{Cnt, [#request{remote_mid = RMid} = _Req]} when 
	(is_integer(Cnt) andalso 
	 ((RMid =:= preliminary_mid) orelse 
	  (RMid =:= CH#megaco_conn_handle.remote_mid))) -> 
	    ?TC_AWAIT_REPLY_EVENT(false),
	    %% Ok, someone got there before me, now what?
	    %% This is a plain old raise condition
	    ?report_important(CD, "trans reply - raise condition", 
			      [T, {request_lockcnt, Cnt}]),
	    megaco_monitor:request_lockcnt_dec(TransId);

	%% no counter
	{_Cnt, [#request{remote_mid = RMid} = Req]} when 
	 ((RMid =:= preliminary_mid) orelse 
	  (RMid =:= CH#megaco_conn_handle.remote_mid)) -> 
	    ?TC_AWAIT_REPLY_EVENT(false),
	    %% The counter does not exist. 
	    %% This can only mean a code upgrade raise condition.
	    %% That is, this request record was created before 
	    %% this feature (the counters) was instroduced.
	    %% The simples solution is this is to behave exactly as
	    %% before, that is pass it along, and leave it to the 
	    %% user to figure out.

	    %% Just in case conn_data got update after our lookup
	    %% but before we looked up the request record, we
	    %% check the cancel field again.
	    ?report_verbose(CD, "trans reply - old style", [T]),
	    case megaco_config:conn_info(CD, cancel) of
		true ->
		    megaco_monitor:request_lockcnt_del(TransId), 
		    do_handle_reply_cancel(CD, Req, T);
		false ->
		    do_handle_reply(CD, Req, TransId, T, Extra)
	    end;

	{Cnt, [#request{user_mod     = UserMod,
			user_args    = UserArgs, 
			reply_action = Action, 
			reply_data   = UserData,
			remote_mid   = RMid}]} ->
	    ?report_trace(CD, 
			  "received trans reply with invalid remote mid", 
			  [{transaction,     T}, 
			   {remote_mid,      RMid}, 
			   {request_lockcnt, Cnt}]),
	    if
		is_integer(Cnt) ->
		    megaco_monitor:request_lockcnt_dec(TransId);
		true ->
		    ok
	    end,
	    WrongMid = CH#megaco_conn_handle.remote_mid,
	    T2 = transform_transaction_reply_enc(CD#conn_data.protocol_version,
						 T),
	    UserReply = {error, {wrong_mid, WrongMid, RMid, T2}}, 
	    CD2 = CD#conn_data{user_mod     = UserMod,
			       user_args    = UserArgs,
			       reply_action = Action,
			       reply_data   = UserData},
	    return_reply(CD2, TransId, UserReply, Extra);

	{Cnt, []} when is_integer(Cnt) ->
	    ?TC_AWAIT_REPLY_EVENT(undefined),
	    ?report_trace(CD, "trans reply (no receiver)", 
			  [T, {request_lockcnt, Cnt}]),
	    megaco_monitor:request_lockcnt_dec(TransId),
	    return_unexpected_trans(CD, T, Extra);

	%% No counter
	{_Cnt, []} -> 
	    ?TC_AWAIT_REPLY_EVENT(undefined),
	    ?report_trace(CD, "trans reply (no receiver)", [T]),
	    return_unexpected_trans(CD, T, Extra)
    end.

do_handle_reply_cancel(CD, #request{user_mod     = UserMod,
				    user_args    = UserArgs, 
				    reply_action = Action, 
				    reply_data   = UserData}, T) ->
    CD2 = CD#conn_data{user_mod     = UserMod,
		       user_args    = UserArgs,
		       reply_action = Action,
		       reply_data   = UserData},
    return_unexpected_trans(CD2, T).

%% Plain old handling of incomming replies
do_handle_reply(CD, 
		#request{timer_ref        = {_Type, Ref}, % OTP-4843
			 user_mod         = UserMod,
			 user_args        = UserArgs, 
			 reply_action     = Action, 
			 reply_data       = UserData,
			 keep_alive_timer = RKAT}, 
		TransId, T, Extra) 
  when ((RKAT =:= plain) orelse (Action =:= call)) ->
    %% Don't care about Req and Rep version diff
    ?report_trace(CD, "trans reply", [T]),

    %% This is the first reply (maybe of many)
    megaco_monitor:delete_request(TransId),
    megaco_monitor:request_lockcnt_del(TransId), 
    megaco_monitor:cancel_apply_after(Ref),           % OTP-4843
    megaco_config:del_pending_counter(recv, TransId), % OTP-7189

    %% Send acknowledgement
    maybe_send_ack(T#megaco_transaction_reply.immAckRequired, CD),

    UserReply =
	case T#megaco_transaction_reply.transactionResult of
	    {transactionError, Reason} ->
		{error, Reason};
	    {actionReplies, Replies} ->
		{ok, Replies}
	end,
    CD2 = CD#conn_data{user_mod     = UserMod,
		       user_args    = UserArgs,
		       reply_action = Action,
		       reply_data   = UserData},
    return_reply(CD2, TransId, UserReply, Extra);

%% This may be the first reply (of maybe many) 
do_handle_reply(CD, 
		#request{user_mod       = UserMod,
			 user_args      = UserArgs, 
			 reply_action   = Action, 
			 reply_data     = UserData,
			 keep_alive_ref = undefined} = Req, 
		TransId, T, Extra) ->
    %% Don't care about Req and Rep version diff
    ?report_trace(CD, "trans reply", [T]),

    %% Could be the first reply, in which case we shall start the 
    %% Request Keep Alive timer...
    %% This could happen for more than one (1) reply though, so
    %% we need to check if the counter value actually equals one (1)!
    
    ReplyNo = 
	create_or_maybe_increment_request_keep_alive_counter(CD, TransId), 
    if 
	(ReplyNo =:= 1) ->
	    %% This *is* the first reply!!
	    %% 1) Stop resend timer
	    {_Type, Ref} = Req#request.timer_ref,             % OTP-4843
	    megaco_monitor:cancel_apply_after(Ref),           % OTP-4843
	
	    %% 2) Delete pending counter
	    megaco_config:del_pending_counter(recv, TransId), % OTP-7189
	    
	    %% 3) Start request keep alive timer
	    ConnHandle = CD#conn_data.conn_handle, 
	    RKATimer   = Req#request.keep_alive_timer, 
	    {RKAWaitFor, _} = megaco_timer:init(RKATimer),
	    RKARef = megaco_monitor:apply_after(?MODULE, 
						request_keep_alive_timeout, 
						[ConnHandle, TransId], 
						RKAWaitFor), 

	    %% 4) Maybe send acknowledgement (three-way-handshake)
	    maybe_send_ack(T#megaco_transaction_reply.immAckRequired, CD),

	    %% 5) And finally store the updated request record
	    Req2 = Req#request{keep_alive_ref = RKARef},
	    megaco_monitor:insert_request(Req2);

	true ->
	    ok
    end,

    UserReply =
	case T#megaco_transaction_reply.transactionResult of
	    {transactionError, Reason} ->
		{error, ReplyNo, Reason};
	    {actionReplies, Replies} ->
		{ok, ReplyNo, Replies}
	end,
    CD2 = CD#conn_data{user_mod     = UserMod,
		       user_args    = UserArgs,
		       reply_action = Action,
		       reply_data   = UserData},
    return_reply(CD2, TransId, UserReply, Extra);

%% This is *not* the first reply (of many) 
do_handle_reply(CD, #request{user_mod     = UserMod,
			     user_args    = UserArgs, 
			     reply_action = Action, 
			     reply_data   = UserData}, TransId, T, Extra) ->
    %% Don't care about Req and Rep version diff
    ?report_trace(CD, "trans reply (first reply already delivered)", [T]),

    ReplyNo = increment_request_keep_alive_counter(CD, TransId), 

    UserReply =
	case T#megaco_transaction_reply.transactionResult of
	    {transactionError, Reason} ->
		{error, ReplyNo, Reason};
	    {actionReplies, Replies} ->
		{ok, ReplyNo, Replies}
	end,
    CD2 = CD#conn_data{user_mod     = UserMod,
		       user_args    = UserArgs,
		       reply_action = Action,
		       reply_data   = UserData},
    return_reply(CD2, TransId, UserReply, Extra).

is_all_segments(Segs) ->
    Sorted = lists:sort(Segs),
    {is_all_segments(Sorted, 1, lists:last(Sorted)), Sorted}.

is_all_segments([Last], Last, Last) ->
    true;
is_all_segments([_], _, _) ->
    false;
is_all_segments([SN|Segs], SN, Last) when (SN < Last) ->
    is_all_segments(Segs, SN+1, Last);
is_all_segments([SN1|_], SN2, _Last) when SN1 =/= SN2 ->
    false.


handle_segment_reply(CD, 
		     #'SegmentReply'{transactionId        = TransId,
				     segmentNumber        = SN, 
				     segmentationComplete = SC}, Extra) ->
    ?rt2("handle segment reply", [{trans_id, TransId}, 
				  {segment_no, SN}, 
				  {segmentation_complete, SC}]),
    TransId2 = to_remote_trans_id(CD#conn_data{serial = TransId}),
    case lookup_reply(CD, TransId2) of
	{_Converted, 
	 #reply{bytes    = Sent,
		segments = []} = Rep} when is_list(Sent) ->
	    ?rt2("no unsent segments", [Sent]),
	    handle_segment_reply_callback(CD, TransId, SN, SC, Extra),
	    case lists:keysearch(SN, 1, Sent) of
		{value, {SN, _Bin, SegTmr}} ->
		    megaco_monitor:cancel_apply_after(SegTmr), %% BMK BMK
		    case lists:keydelete(SN, 1, Sent) of
			[] ->  %% We are done
			    Ref = Rep#reply.timer_ref, 
			    megaco_monitor:cancel_apply_after(Ref),
			    megaco_monitor:update_reply_field(TransId2, 
							      #reply.bytes,
							      []),
			    ok;
			NewSent ->
			    megaco_monitor:update_reply_field(TransId2, 
							      #reply.bytes,
							      NewSent),
			    ok
		    end;
		_ ->
		    ok
	    end;
		
	{_Converted,
	 #reply{bytes    = Sent,
		segments = NotSent}} when is_list(Sent) andalso 
					  is_list(NotSent) ->
	    ?rt2("unsent segments", [Sent, NotSent]),
	    handle_segment_reply_callback(CD, TransId, SN, SC, Extra),
	    case lists:keysearch(SN, 1, Sent) of
		{value, {SN, _Bin, SegTmr}} ->
		    megaco_monitor:cancel_apply_after(SegTmr), %% BMK BMK
		    NewSent = lists:keydelete(SN, 1, Sent),
		    [{SN2, Bin2}|NewNotSent] = NotSent,
		    case send_reply_segment(CD, "send trans reply segment", 
					    SN2, Bin2) of
			{ok, Bin3} ->
			    ?rt2("another segment sent", [Bin3]),
			    NewSent2 = [{SN2, Bin3, undefined}|NewSent],
			    NewFields = 
				[{#reply.bytes,    NewSent2},
				 {#reply.segments, NewNotSent}],
			    megaco_monitor:update_reply_fields(TransId2, 
							       NewFields),
			    ok;
			Error ->
			    incNumErrors(CD#conn_data.conn_handle),
			    ?report_important(CD, "failed sending segment", 
					      [{segment_no, SN2}, 
					       {error,      Error}]),
			    error_msg("failed sending transaction reply [~w] "
				      "segment [~w]: ~w", 
				      [TransId, SN2, Error]),
			    megaco_monitor:update_reply_field(TransId2, 
							      #reply.bytes,
							      NewSent),
			    ok
		    end;
		_ ->
		    ok
	    end;

	{_Converted,
	 #reply{state = State}} ->
	    %% We received a segment reply for a segmented reply we have 
	    %% not yet sent? This is either some sort of race condition 
	    %% or the "the other side" is really confused.
	    %% Ignore the message but issue a warning just in case...
	    warning_msg("received unexpected segment reply: "
			"~n   Transaction Id:        ~p"
			"~n   Segment Number:        ~p"
			"~n   Segmentation Complete: ~p"
			"~n   Reply state:           ~p", 
			[TransId2, SN, SC, State]),
	    ignore;

	[] ->
	    ignore

    end.
    

%% 
%% This should be passed on to the user only if the user wish it
%% (sri = segment reply indication)
%% 
handle_segment_reply_callback(#conn_data{segment_reply_ind = true,
					 conn_handle       = ConnHandle,
					 protocol_version  = Version,
					 user_mod          = UserMod,
					 user_args         = UserArgs},
			      TransId, SN, SC, Extra) ->
    Args = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, TransId, SN, SC | UserArgs];
	    _ ->
		[ConnHandle, Version, TransId, SN, SC, Extra | UserArgs]
	end,
    (catch apply(UserMod, handle_segment_reply, Args));
handle_segment_reply_callback(_CD, _TransId, _SN, _SC, _Extra) ->
    ok.


handle_acks([{ConnData, Rep, T} | Rest], Extra)
  when Rep#reply.state == waiting_for_ack ->
    handle_ack(ConnData, ok, Rep, T, Extra),
    handle_acks(Rest, Extra);
handle_acks([], _Extra) ->
    ok.

%% If the reply to which this is the ack was segmented,
%% then we also need to check that we have received all 
%% the segment-replies. If not, an error callback call 
%% shall be made instead.
handle_ack(ConnData, AckStatus, 
	   #reply{trans_id          = TransId,
		  bytes             = Bytes, 
		  timer_ref         = ReplyRef,
		  pending_timer_ref = PendingRef,  %% BMK Still running?
		  ack_action        = AckAction}, T, Extra) 
  when is_binary(Bytes) orelse (Bytes =:= undefined) ->
    handle_ack_cleanup(TransId, ReplyRef, PendingRef),
    handle_ack_callback(ConnData, AckStatus, AckAction, T, Extra);

handle_ack(ConnData, AckStatus, 
	   #reply{trans_id          = TransId,
		  bytes             = [], 
		  segments          = [], 
		  timer_ref         = ReplyRef,
		  pending_timer_ref = PendingRef,  %% BMK Still running?
		  ack_action        = AckAction}, T, Extra) ->
    handle_ack_cleanup(TransId, ReplyRef, PendingRef),
    handle_ack_callback(ConnData, AckStatus, AckAction, T, Extra);

handle_ack(ConnData, OrigAckStatus, 
	   #reply{trans_id          = TransId,
		  bytes             = SegSent, 
		  segments          = NotSent, 
		  timer_ref         = ReplyRef,
		  pending_timer_ref = PendingRef,  %% BMK Still running?
		  ack_action        = OrigAckAction}, T, Extra) 
  when is_list(SegSent) andalso is_list(NotSent) ->
    SN_NotAcked = [SN || {SN, _, _} <- SegSent],
    SN_NotSent  = [SN || {SN, _}    <- NotSent],
    AckStatus   = {error, {segment_failure, 
			   [{original_ack_status, OrigAckStatus},
			    {segments_not_acked,  SN_NotAcked},
			    {segments_not_sent,   SN_NotSent}]}},
    AckAction   = 
	case OrigAckAction of
	    {handle_ack, _} ->
		OrigAckAction;
	    _ ->
		{handle_ack, segmented_reply} 
	end,
    cancel_segment_timers(SegSent),
    handle_ack_cleanup(TransId, ReplyRef, PendingRef),
    handle_ack_callback(ConnData, AckStatus, AckAction, T, Extra).

handle_ack_cleanup(TransId, ReplyRef, PendingRef) ->
    megaco_monitor:cancel_apply_after(ReplyRef),
    megaco_monitor:cancel_apply_after(PendingRef),
    megaco_monitor:delete_reply(TransId),
    megaco_config:del_pending_counter(sent, TransId). %% BMK: Still existing?
    
cancel_segment_timers(SegSent) when is_list(SegSent) ->
    Cancel = fun({_, _, Ref}) ->
		     megaco_monitor:cancel_apply_after(Ref)
	     end,
    lists:foreach(Cancel, SegSent);
cancel_segment_timers(_) ->
    ok.

handle_ack_callback(_CD, ok = _AS, discard_ack = _AA, _T, _Extra) ->
    ok;
handle_ack_callback(ConnData, {error, Reason}, discard_ack = AckAction, T, Extra) ->
    ?report_trace(ConnData, "handle ack (no callback)",
		  [T, AckAction, {error, Reason}, Extra]);
handle_ack_callback(ConnData, AckStatus, {handle_ack, AckData}, T, Extra) ->
    ?report_trace(ConnData, "callback: trans ack", [{ack_data, AckData}]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, AckStatus, AckData | UserArgs];
	    _ ->
		[ConnHandle, Version, AckStatus, AckData, Extra | UserArgs]
	end,
    Res = (catch handle_callback(ConnData, UserMod, handle_trans_ack, Args)),
    ?report_debug(ConnData, "return: trans ack", [T, AckData, {return, Res}]),
    case Res of
	ok ->
	    ok;
	_ ->
	    warning_msg("transaction ack callback failed: ~w", [Res]),
	    ok
    end,
    Res.


handle_callback(ConnData, undefined = _UserMod, Func, Args) ->
    ?report_important(ConnData, "callback: unknown callback module", 
		      [{func, Func}, {args, Args}]),
    ok;
handle_callback(_ConnData, UserMod, Func, Args) ->
    (catch apply(UserMod, Func, Args)).


handle_message_error(ConnData, _Error, _Extra) 
  when ConnData#conn_data.monitor_ref == undefined_monitor_ref ->
    %% May occur if another process already has setup a
    %% temporary connection, but the handle_connect callback
    %% function has not yet returned before the eager MG
    %% re-sends its initial service change message.
    ignore;
handle_message_error(ConnData, Error, Extra) ->
    ?report_trace(ConnData, "callback: message error", [Error]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, Error | UserArgs];
	    _ ->
		[ConnHandle, Version, Error, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_message_error, Args)),
    ?report_debug(ConnData, "return: message error", [Error, {return, Res}]),
    case Res of
	ok ->
	    ok;
	_ ->
	    warning_msg("message error callback failed: ~w", [Res]),
	    ok
    end,
    Res.

handle_disconnect_callback(ConnData, UserReason)
  when is_record(ConnData, conn_data) ->
    ?report_trace(ConnData, "callback: disconnect", [{reason, UserReason}]),
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    Args       = [ConnHandle, Version, UserReason | UserArgs], 
    Res = (catch apply(UserMod, handle_disconnect, Args)),
    ?report_debug(ConnData, "return: disconnect", [{reason, UserReason}, {return, Res}]),
    case Res of
	ok ->
	    ok;
	_ ->
	    warning_msg("disconnect callback failed: ~w", [Res]),
	    ok
    end,
    Res.


%%----------------------------------------------------------------------
%% Test "outgoing" messages
%%----------------------------------------------------------------------

%% test_request/5 -> {MegacoMessage, EncodingRes}
%% 
%% This function is only intended for testing 
%% (e.g. answer the question: have I constructed a valid action request?)
%% 
%% It's not exactly the same code as a call to 'call'
%% or 'cast' but close enough.
%% 
test_request(ConnHandle, Actions, 
	     Version, EncodingMod, EncodingConfig)
  when is_record(ConnHandle, megaco_conn_handle) and
       is_integer(Version) andalso is_atom(EncodingMod) ->
    %% Create a fake conn_data structure
    ConnData = #conn_data{serial           = 1, 
			  protocol_version = Version,
			  conn_handle      = ConnHandle,
			  auth_data        = asn1_NOVALUE,
			  encoding_mod     = EncodingMod,
			  encoding_config  = EncodingConfig},

    TRs = test_req_compose_transactions(ConnData, Actions),
    Body = {transactions, TRs},
    MegaMsg = megaco_messenger_misc:compose_message(ConnData, Version, Body),
    EncodeRes = megaco_messenger_misc:encode_message(ConnData, MegaMsg),
    {MegaMsg, EncodeRes}.


test_req_compose_transactions(ConnData, [A|_] = ActionsList) when is_list(A) ->
    LastSerial = ConnData#conn_data.serial,
    test_req_compose_transactions(LastSerial, lists:reverse(ActionsList), []);
test_req_compose_transactions(#conn_data{serial = Serial}, Actions) ->
    TR   = #'TransactionRequest'{transactionId = Serial,
				 actions       = Actions},
    [{transactionRequest, TR}].

test_req_compose_transactions(_Serial, [], Acc) ->
    lists:reverse(Acc);
test_req_compose_transactions(Serial, [A|As], Acc) ->
    TR = #'TransactionRequest'{transactionId = Serial,
                               actions       = A},
    test_req_compose_transactions(Serial, As, [{transactionRequest, TR}|Acc]).


test_reply(ConnHandle, Version, EncodingMod, EncodingConfig, Error) 
  when is_record(Error, 'ErrorDescriptor') ->
    Reply = {transactionError, Error},
    test_reply_encode(ConnHandle, Version, EncodingMod, EncodingConfig, Reply);
test_reply(ConnHandle, Version, EncodingMod, EncodingConfig, Replies) 
  when is_list(Replies) ->
    Reply = {actionReplies, Replies},
    test_reply_encode(ConnHandle, Version, EncodingMod, EncodingConfig, Reply).

test_reply_encode(ConnHandle, Version, EncodingMod, EncodingConfig, Reply) ->
    ImmAck    = asn1_NOVALUE,
    Serial    = 1,
    %% Create a fake conn_data structure
    CD        = #conn_data{serial           = Serial, 
			   protocol_version = Version,
			   conn_handle      = ConnHandle,
			   auth_data        = asn1_NOVALUE,
			   encoding_mod     = EncodingMod,
			   encoding_config  = EncodingConfig},
    TR0       = #megaco_transaction_reply{transactionId     = Serial, 
					  immAckRequired    = ImmAck, 
					  transactionResult = Reply}, 
    TR        = megaco_messenger_misc:transform_transaction_reply(CD, TR0),
    Body      = {transactions, [{transactionReply, TR}]},
    MegaMsg   = megaco_messenger_misc:compose_message(CD, Version, Body),
    EncodeRes = megaco_messenger_misc:encode_message(CD, MegaMsg),
    {MegaMsg, EncodeRes}.
    

%%----------------------------------------------------------------------
%% Send (or prepare) outgoing messages
%%----------------------------------------------------------------------

%% Description:
%% Encode a list of actions or a list of list of actions for
%% later sending (using call or cast).
%% 
%% encode_actions(CH, Acts, Opts) -> {ok, encoded_actions()} | {error, Reason}
%% CH -> connection_handle()
%% Acts -> action_reqs() | [action_reqs()]
%% action_reqs() -> [action_req()]
%% action_req() -> #'ActionRequest'{}
%% Opts -> [option()]
%% option() -> {Tab, Val}
%% Tag -> atom()
%% Val -> term()
%% encoded_actions() -> binary() | [binary()]
%% Reason -> term()
encode_actions(CH, [A|_] = ActionsList, Opts) 
  when is_record(CH, megaco_conn_handle) andalso is_list(A) ->
    (catch encode_multi_actions(CH, ActionsList, Opts));

encode_actions(CH, [A|_] = Actions, Opts) 
  when is_record(CH, megaco_conn_handle) andalso is_tuple(A) ->
    do_encode_actions(CH, Actions, Opts).
    
encode_multi_actions(CH, ActionsList, Opts) ->
    case prepare_req_send_options(CH, Opts) of
	{ok, CD} ->
	    ActsList = [encode_multi_actions(CD, Acts) || Acts <- ActionsList],
	    {ok, ActsList};
	Error ->
	    Error
    end.

encode_multi_actions(CD, Actions) ->
    case megaco_messenger_misc:encode_actions(CD, 
					      "encode multi actions", 
					      Actions) of
	{ok, Bin} ->
	    Bin;
	Error ->
	    throw(Error)
    end.

do_encode_actions(CH, Actions, Opts) 
  when is_record(CH, megaco_conn_handle) ->
    case prepare_req_send_options(CH, Opts) of
        {ok, CD} ->
	    megaco_messenger_misc:encode_actions(CD, 
						 "encode actions", 
						 Actions);
	Error ->
	    Error
    end.

prepare_req_send_options(CH, Opts) ->
    case megaco_config:lookup_local_conn(CH) of
        [CD] ->
            override_req_send_options(any, CD, Opts);
        [] ->
            {error, {not_found, conn_data}}
    end.
    

call(ConnHandle, Actions, Options) ->
    case lists:keymember(reply_data, 1, Options) of
	true ->
	    {error, {bad_option, reply_data}};
	false ->
	    Self          = self(), 
	    ProxyFun      = fun() -> call_proxy(Self) end,
	    {Proxy, MRef} = erlang:spawn_monitor(ProxyFun),
	    Options2      = [{reply_data, Proxy} | Options],
	    call_or_cast(call, ConnHandle, Actions, Options2, MRef)
    end.

cast(ConnHandle, Actions, Options) ->
    call_or_cast(cast, ConnHandle, Actions, Options, undefined).

%% In a transaction there can be several actions, so if the 
%% First element of the Actions list is an ''ActionRequest''
%% record this a list of ActionRequest's for one Transaction
%% request. If on the other hand this is not the case, then
%% the Actions list is assumed to be a list of list of
%% ActionRequest. That is, action requests for several transactions.
%% It could also be a binary or a list of binaries (if 
%% the actions has already been encoded).
call_or_cast(CallOrCast, ConnHandle, [A|_] = Actions, Options, ProxyMon) 
  when is_tuple(A) ->
    %% Just one transaction
    case call_or_cast(CallOrCast, ConnHandle, [Actions], Options, ProxyMon) of
	ok ->
	    ok;
	{error, Reason} ->
	    {error, Reason};
	{Version, [Reply]} when is_integer(Version) ->
	    {Version, Reply};
	{Version, Error} when is_integer(Version) ->
	    {Version, Error}
    end;

call_or_cast(CallOrCast, ConnHandle, Actions, Options, ProxyMon) 
  when is_binary(Actions) ->
    %% Just one transaction (although the actions has already been encoded)
    case call_or_cast(CallOrCast, ConnHandle, [Actions], Options, ProxyMon) of
	ok ->
	    ok;
	{error, Reason} ->
	    {error, Reason};
	{Version, [Reply]} when is_integer(Version) ->
	    {Version, Reply};
	{Version, Error} when is_integer(Version) ->
	    {Version, Error}
    end;

call_or_cast(CallOrCast, ConnHandle, ActionsList, Options, ProxyMon)
  when is_record(ConnHandle, megaco_conn_handle) ->
    case prepare_req_send_options(CallOrCast, 
				  ConnHandle, Options, ActionsList) of
        {ok, ConnData} ->
	    ?report_trace(ConnData, "call_or_cast - options prepared", []),
            case encode_requests(ConnData, ActionsList) of
                {ok, TRs, BinOrBins} -> 
		    ?report_trace(ConnData, 
				  "call_or_cast - request encoded", []),
                    send_request(ConnData, ConnHandle, 
				 TRs, CallOrCast, BinOrBins),
		    case CallOrCast of
			call -> 
			    TransIds = to_local_trans_id(ConnData, TRs),
			    wait_for_reply(ConnData, TransIds, ProxyMon);
			cast -> 
			    ok
		    end;
                {error, Reason} ->
		    call_proxy_cleanup(ConnData, ProxyMon),
		    Version = ConnData#conn_data.protocol_version,
                    return_error(CallOrCast, Version, {error, Reason})
            end;
        {error, Reason} ->
	    call_proxy_cleanup(Options, ProxyMon),
            return_error(CallOrCast, 1, {error, Reason})
    end;
call_or_cast(CallOrCast, ConnHandle, _Actions, Options, ProxyMon) ->
    call_proxy_cleanup(Options, ProxyMon),
    return_error(CallOrCast, 1, {error, {bad_megaco_conn_handle, ConnHandle}}).


return_error(Action, Version, Error) ->
    case Action of
	call -> {Version, Error};
	cast -> Error
    end.

wait_for_reply(CD, TransIds, ProxyMon) ->
    ProxyPid = CD#conn_data.reply_data,
    ProxyPid ! {go, self(), CD, TransIds},
    receive
	{reply, ProxyPid, Reply} ->
	    erlang:demonitor(ProxyMon, [flush]),
	    Reply;
	{'DOWN', ProxyMon, process, ProxyPid, Info} ->
	    UserReply = {error, {call_proxy_crash, Info}}, 
	    {CD#conn_data.protocol_version, UserReply}
    end.
    

call_proxy_cleanup(#conn_data{reply_data = ProxyPid}, ProxyMon) ->
    do_call_proxy_cleanup(ProxyPid, ProxyMon);
call_proxy_cleanup(Options, ProxyMon) when is_list(Options) ->
    ProxyPid = 
	case lists:keysearch(reply_data, 1, Options) of
	    {value, {reply_data, Data}} ->
		Data;
	    _ ->
		undefined
	end,
    do_call_proxy_cleanup(ProxyPid, ProxyMon);
call_proxy_cleanup(ProxyPid, ProxyMon) ->
    do_call_proxy_cleanup(ProxyPid, ProxyMon).

do_call_proxy_cleanup(ProxyPid, ProxyMon) ->
    maybe_demonitor(ProxyMon),
    maybe_stop_proxy(ProxyPid),
    ok.
   
maybe_demonitor(undefined) ->
    ok;
maybe_demonitor(Mon) ->
    (catch erlang:demonitor(Mon, [flush])),
    ok.

maybe_stop_proxy(Pid) when is_pid(Pid) ->
    Pid ! {stop, self()},
    ok;
maybe_stop_proxy(_) ->
    ok.


call_proxy(Parent) ->
    receive
	{go, Parent, CD, TransIds} ->
	    call_proxy(Parent, CD, TransIds);
	{stop, Parent} ->
	    exit(normal)
    end.

call_proxy(Parent, CD, TransIds) ->
    Reply = proxy_wait_for_reply(CD, TransIds, []),
    Parent ! {reply, self(), Reply},
    call_proxy_gc(CD, CD#conn_data.call_proxy_gc_timeout).

call_proxy_gc(CD, Timeout) when (Timeout > 0) ->
    T = t(),
    receive
	{?MODULE, TransId, Version, Result} -> % Old format
	    CD2 = CD#conn_data{protocol_version = Version}, 
	    Extra = ?default_user_callback_extra, 
	    return_unexpected_trans_reply(CD2, TransId, Result, Extra),
	    call_proxy_gc(CD, Timeout - (t() - T));

	{?MODULE, TransId, Version, Result, Extra} ->
	    CD2 = CD#conn_data{protocol_version = Version}, 
	    return_unexpected_trans_reply(CD2, TransId, Result, Extra),
	    call_proxy_gc(CD, Timeout - (t() - T))

    after Timeout ->
	    exit(normal)
    end;
call_proxy_gc(_CD, _Timeout) ->
    exit(normal).

proxy_wait_for_reply(_CD, [], Replies0) ->
    % Make sure they come in the same order as the requests where sent
    Replies1 = lists:keysort(2, Replies0), 
    %% Must all be the same version
    [{Version, _, _}|_] = Replies1,
    Replies2 = [Result || {_Version, _TransId, Result} <- Replies1], 
    {Version, Replies2};
proxy_wait_for_reply(CD, TransIds, Replies) -> 
    receive
	{?MODULE, TransId, Version, Reply} -> % Old format
	    {TransIds2, Replies2} = 
		wfr_handle_reply(CD, 
				 TransIds, TransId, 
				 Version, Replies, Reply),
	    proxy_wait_for_reply(CD, TransIds2, Replies2);
	    
	{?MODULE, TransId, Version, Reply, Extra} -> 
	    {TransIds2, Replies2} = 
		wfr_handle_reply(CD, 
				 TransIds, TransId, 
				 Version, Replies, Reply, Extra),
	    proxy_wait_for_reply(CD, TransIds2, Replies2)
    end.

wfr_handle_reply(CD, TransIds, TransId, Version, Replies, Reply) ->
    Extra = ?default_user_callback_extra,
    wfr_handle_reply(CD, TransIds, TransId, Version, Replies, Reply, Extra).

wfr_handle_reply(CD, TransIds, TransId, Version, Replies, Reply, Extra) ->
    %% Is this meant for us?
    case lists:member(TransId, TransIds) of
	true -> % Yep
	    wfr_update(TransIds, TransId, Version, Replies, Reply, Extra);
	false -> % Nop
	    CD2 = CD#conn_data{protocol_version = Version}, 
	    return_unexpected_trans_reply(CD2, TransId, Reply, Extra),
	    {TransIds, Replies}
    end.
    
wfr_mk_reply(Version, TransId, Result, ?default_user_callback_extra = _Extra) ->
    {Version, TransId, Result};
wfr_mk_reply(Version, TransId, Result0, Extra) ->
    Result = list_to_tuple(lists:append(tuple_to_list(Result0), [Extra])),
    {Version, TransId, Result}.

%% Last segment of a reply
%% transactionResult "=" actionReplies
wfr_update(TransIds, TransId, Version, Results, {ok, {SegNo, Last, ARs}}, Extra) 
  when is_integer(SegNo) andalso (Last == true) ->
    TransIds2 = lists:delete(TransId, TransIds), 
    case lists:keysearch(TransId, 2, Results) of
	
	%% All segments ok (actionReplies)
	{value, {V, TransId, {ok, SegReps}}} ->
	    SegReps2 = lists:keysort(1, [{SegNo, ARs}|SegReps]),
	    Rep      = wfr_mk_reply(V, TransId, {ok, SegReps2}, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};
	
	%% Atleast one segment error (transactionError)
	{value, {V, TransId, {error, {segment, OkSegs, ErrSegs}}}} ->
	    OkSegs2  = lists:keysort(1, [{SegNo, ARs}|OkSegs]),
	    ErrSegs2 = lists:keysort(1, ErrSegs),
	    Error    = {error, {segment, OkSegs2, ErrSegs2}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};
	
	false ->
	    %% First and only segment
	    Rep = wfr_mk_reply(Version, TransId, {ok, [{SegNo, ARs}]}, Extra),
	    {TransIds2, [Rep | Results]}
    
    end;

%% Last segment of a reply
%% transactionResult "=" transactionError
wfr_update(TransIds, TransId, Version, Results, {error, {SegNo, Last, ED}}, Extra) 
  when is_integer(SegNo) andalso (Last == true) ->
    TransIds2 = lists:delete(TransId, TransIds), 
    case lists:keysearch(TransId, 2, Results) of
	
	%% First segment with error (transactionError)
	{value, {V, TransId, {ok, SegReps}}} ->
	    OkSegs   = lists:keysort(1, [{SegNo, ED}|SegReps]),
	    ErrSegs  = [{SegNo, ED}], 
	    Error    = {error, {segment, OkSegs, ErrSegs}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};

	%% Another segment with error (transactionError)
	{value, {V, TransId, {error, {segment, OkSegs, ErrSegs}}}} ->
	    OkSegs2  = lists:keysort(1, OkSegs),
	    ErrSegs2 = lists:keysort(1, [{SegNo, ED}|ErrSegs]),
	    Error    = {error, {segment, OkSegs2, ErrSegs2}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};

	false ->
	    %% First and only segment
	    OkSegs  = [],
	    ErrSegs = [{SegNo, ED}], 
	    Error   = {error, {segment, OkSegs, ErrSegs}}, 
	    Rep     = wfr_mk_reply(Version, TransId, Error, Extra),
	    {TransIds2, [Rep]}

    end;

%% One segment of a reply
%% transactionResult "=" actionReplies
wfr_update(TransIds, TransId, Version, Results, {ok, {SegNo, _Last, ARs}}, Extra) 
  when is_integer(SegNo) ->
    case lists:keysearch(TransId, 2, Results) of
	
	%% All segments ok (actionReplies)
	{value, {V, TransId, {ok, SegReps}}} ->
	    SegReps2 = [{SegNo, ARs}|SegReps],
	    Rep      = wfr_mk_reply(V, TransId, {ok, SegReps2}, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds, Results2};
	
	%% Atleast one segment error (transactionError)
	{value, {V, TransId, {error, {segment, OkSegs, ErrSegs}}}} ->
	    OkSegs2  = [{SegNo, ARs}|OkSegs],
	    Error    = {error, {segment, OkSegs2, ErrSegs}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds, Results2};
	
	false ->
	    %% First and only segment
	    Rep = wfr_mk_reply(Version, TransId, {ok, [{SegNo, ARs}]}, Extra),
	    {TransIds, [Rep | Results]}
    
    end;

%% One segment of a reply
%% transactionResult "=" transactionError
wfr_update(TransIds, TransId, Version, Results, {error, {SegNo, _Last, ED}}, Extra) 
  when is_integer(SegNo) ->
    case lists:keysearch(TransId, 2, Results) of
	
	%% First segment with error (transactionError)
	{value, {V, TransId, {ok, OkSegs}}} ->
	    ErrSegs  = [{SegNo, ED}], 
	    Error    = {error, {segment, OkSegs, ErrSegs}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds, Results2};

	%% Another segment with error (transactionError)
	{value, {V, TransId, {error, {OkSegs, ErrSegs}}}} ->
	    ErrSegs2 = [{SegNo, ED}|ErrSegs],
	    Error    = {error, {segment, OkSegs, ErrSegs2}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds, Results2};

	false ->
	    %% First segment
	    OkSegs  = [],
	    ErrSegs = [{SegNo, ED}], 
	    Error   = {error, {segment, OkSegs, ErrSegs}}, 
	    Rep     = wfr_mk_reply(Version, TransId, Error, Extra),
	    {TransIds, [Rep]}

    end;

%% This means that some segments did not make it in time
wfr_update(TransIds, TransId, Version, Results, 
	   {error, {segment_timeout, Missing}}, Extra) ->
    TransIds2 = lists:delete(TransId, TransIds), 
    case lists:keysearch(TransId, 2, Results) of
	
	%% First segment with error (transactionError)
	{value, {V, TransId, {ok, OkSegs}}} ->
	    Error    = {error, {segment_timeout, Missing, OkSegs, []}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};
       
	%% Another segment with error (transactionError)
	{value, {V, TransId, {error, {segment, OkSegs, ErrSegs}}}} ->
	    Error    = {error, {segment_timeout, Missing, OkSegs, ErrSegs}}, 
	    Rep      = wfr_mk_reply(V, TransId, Error, Extra),
	    Results2 = lists:keyreplace(TransId, 2, Results, Rep),
	    {TransIds2, Results2};

	false ->
	    %% First segment
	    Error   = {error, {segment_timeout, Missing, [], []}}, 
	    Rep     = wfr_mk_reply(Version, TransId, Error, Extra), 
	    {TransIds2, [Rep]}

    end;
    
%% And all other results (presumably results without segments).
wfr_update(TransIds, TransId, Version, Results, Result, Extra) ->
    TransIds2 = lists:delete(TransId, TransIds), 
    Results2  = [wfr_mk_reply(Version, TransId, Result, Extra)|Results],
    {TransIds2, Results2}.
    
    
%% TransInfo is either [trans_id()] or a [trans_req()]

%% This is the normal case where we have just one
%% transaction to be sent (using call or cast) using
%% the transaction sender process.
send_request(#conn_data{control_pid  = CP, 
			trans_req    = true, 
			trans_sender = Pid} = CD, 
	     CH, [Serial], Action, [Bin])
  when is_pid(Pid)        andalso 
       is_integer(Serial) andalso 
       (node(CP) =:= node()) ->

    ?report_trace(CD, 
		  "send_request - one transaction via trans-sender", 
		  [Serial]),

    #conn_data{request_timer       = InitTimer,
	       long_request_timer  = LongTimer} = CD,
    TransId = to_local_trans_id(CH, Serial),
    insert_request(CD, CH, TransId, Action, {Serial, Bin},
		   InitTimer, LongTimer),
    megaco_trans_sender:send_req(Pid, Serial, Bin);

%% This is the general case where we have several transactions
%% beeing sent (using call or cast) at once using
%% the transaction sender process.
send_request(#conn_data{control_pid  = CP, 
			trans_req    = true, 
			trans_sender = Pid} = CD, 
	     CH, TransInfo, Action, Bins)
  when is_pid(Pid)   andalso 
       is_list(Bins) andalso 
       (node(CP) =:= node()) ->

    ?report_trace(CD, 
		  "send_request - multi transactions via trans_sender", 
		  [TransInfo, Pid]),

    #conn_data{request_timer       = InitTimer,
	       long_request_timer  = LongTimer} = CD,
    insert_requests(CD, CH, TransInfo, Action, Bins,
		    InitTimer, LongTimer),
    megaco_trans_sender:send_reqs(Pid, TransInfo, Bins);

%% This is the case when one or more transactions is
%% beeing sent in one message immediatelly (not using
%% the transaction sender process. E.g. the binary is 
%% this encoded message.
send_request(#conn_data{control_pid = CP} = CD, 
	     CH, TRs, Action, Bin)
  when is_list(TRs)   andalso 
       is_binary(Bin) andalso 
       (node(CP) =:= node()) ->

    %%     d("send_request -> entry with"
    %%       "~n   TRs: ~p", [TRs]),

    ?report_trace(CD, "send_request - multi transaction", [TRs]),

    #conn_data{request_timer       = InitTimer,
	       long_request_timer  = LongTimer} = CD,
    insert_requests(CD, CH, TRs, Action, Bin,
		    InitTimer, LongTimer),
    case megaco_messenger_misc:send_message(CD, false, Bin) of
	{error, Reason} ->
	    cancel_requests(CD, TRs, Reason);
	{ok, _} ->
	    ignore
    end;

%% This is the case where we are not on the node where the
%% transport process run.
send_request(#conn_data{control_pid = CP} = CD, 
	     CH, TransInfo, Action, Bin) 
  when node(CP) =/= node() ->

    ?report_trace(CD, "send_request - remote", [TransInfo]),

    InitTimer = infinity,
    LongTimer = infinity,
    insert_requests(CD, CH, TransInfo, Action, Bin,
		    InitTimer, LongTimer),
    Node = node(CP),
    Args = [node(), CD, TransInfo, Bin],
    rpc:cast(Node, ?MODULE, send_request_remote, Args).


insert_requests(_, _, [], _, _, _, _) ->
    ok;

insert_requests(ConnData, ConnHandle, [Serial|Serials], 
		Action, [Bin|Bins], InitTimer, LongTimer) 
  when is_integer(Serial) andalso is_binary(Bin) ->
    TransId = to_local_trans_id(ConnHandle, Serial),
    insert_request(ConnData, ConnHandle, 
		   TransId, Action, Bin, InitTimer, LongTimer),

    insert_requests(ConnData, ConnHandle, Serials, Action, Bins, 
		    InitTimer, LongTimer);

insert_requests(ConnData, ConnHandle, 
		[{transactionRequest, TR}|TRs], 
		Action, Bin, InitTimer, LongTimer) 
  when is_record(TR, 'TransactionRequest') andalso is_binary(Bin) ->
    #'TransactionRequest'{transactionId = Serial} = TR,
    TransId = to_local_trans_id(ConnHandle, Serial),
    insert_request(ConnData, ConnHandle, 
		   TransId, Action, TR, InitTimer, LongTimer),

    insert_requests(ConnData, ConnHandle, TRs, Action, Bin, 
		    InitTimer, LongTimer).


insert_request(ConnData, ConnHandle, TransId, 
	       Action, Data, InitTimer, LongTimer) ->
    %% We dont check the result of the lock-counter creation because
    %% the only way it could already exist is if the transaction-id
    %% range has wrapped and an old counter was not deleted.
    megaco_monitor:request_lockcnt_cre(TransId),

    #megaco_conn_handle{remote_mid = RemoteMid} = ConnHandle,
    #conn_data{protocol_version           = Version,
	       user_mod                   = UserMod,
	       user_args                  = UserArgs,
	       send_handle                = SendHandle,
	       reply_data                 = ReplyData,
	       segment_recv_timer         = InitSegTimer,
	       request_keep_alive_timeout = RKATimer} = ConnData,
    {WaitFor, CurrTimer} = megaco_timer:init(InitTimer),
    M   = ?MODULE,
    F   = request_timeout,
    A   = [ConnHandle, TransId],
    Ref = megaco_monitor:apply_after(M, F, A, WaitFor),
    Req = #request{trans_id         = TransId, 
		   remote_mid       = RemoteMid,
                   timer_ref        = ?SIM({short, Ref}, init_request_timer),
                   init_timer       = InitTimer,
                   init_long_timer  = LongTimer,
                   curr_timer       = CurrTimer,
                   version          = Version,
                   bytes            = {send, Data},
		   send_handle      = SendHandle,
                   user_mod         = UserMod,
                   user_args        = UserArgs,
                   reply_action     = Action,
                   reply_data       = ReplyData,
		   init_seg_timer   = InitSegTimer,
		   keep_alive_timer = RKATimer},
    megaco_monitor:insert_request(Req). % Timing problem?

    
send_request_remote(ReplyNode, ConnData, TransInfo, Bin) ->
    Action = remote,
    ConnHandle = ConnData#conn_data.conn_handle,
    ConnData2  = ConnData#conn_data{reply_data = ReplyNode},
    send_request(ConnData2, ConnHandle, TransInfo, Action, Bin).

prepare_req_send_options(CallOrCast, ConnHandle, Options, Actions) ->
    %% Ensures that two processes cannot get same transaction id.
    %% Bad send options may cause spurious transaction id to be consumed.
    Incr = number_of_transactions(Actions),
    case megaco_config:incr_trans_id_counter(ConnHandle, Incr) of
        {ok, ConnData} ->
            override_req_send_options(CallOrCast, ConnData, Options);
        {error, Reason} ->
            {error, Reason}
    end.

number_of_transactions([Action|_]) when is_tuple(Action) ->
    1;
number_of_transactions(ActionsList) ->
    length(ActionsList).

override_req_send_options(ReplyAction, ConnData, [{Key, Val} | Tail]) ->
    case Key of
	protocol_version ->
            ConnData2 = ConnData#conn_data{protocol_version = Val},
            override_req_send_options(ReplyAction, ConnData2, Tail);	    
        send_handle ->
            ConnData2 = ConnData#conn_data{send_handle = Val},
            override_req_send_options(ReplyAction, ConnData2, Tail);
        request_timer ->
            case megaco_config:verify_val(Key, Val) of
                true ->
                    ConnData2 = ConnData#conn_data{request_timer = Val},
                    override_req_send_options(ReplyAction, ConnData2, Tail);
                false ->
                    {error, {bad_send_option, {Key, Val}}}
            end;
        long_request_timer ->
            case megaco_config:verify_val(Key, Val) of
                true ->
                    ConnData2 = ConnData#conn_data{long_request_timer = Val},
                    override_req_send_options(ReplyAction, ConnData2, Tail);
                false ->
                    {error, {bad_send_option, {Key, Val}}}
            end;
	call_proxy_gc_timeout when (ReplyAction =:= call) orelse 
				   (ReplyAction =:= any) ->
            case megaco_config:verify_val(Key, Val) of
                true ->
                    ConnData2 = 
			ConnData#conn_data{call_proxy_gc_timeout = Val},
                    override_req_send_options(ReplyAction, ConnData2, Tail);
                false ->
                    {error, {bad_send_option, {Key, Val}}}
            end;
	request_keep_alive_timeout when (ReplyAction =:= cast) orelse 
					(ReplyAction =:= any) ->
            case megaco_config:verify_val(Key, Val) of
                true ->
                    ConnData2 = 
			ConnData#conn_data{request_keep_alive_timeout = Val},
                    override_req_send_options(ReplyAction, ConnData2, Tail);
                false ->
                    {error, {bad_send_option, {Key, Val}}}
            end;
        reply_data ->
            ConnData2 = ConnData#conn_data{reply_data = Val},
            override_req_send_options(ReplyAction, ConnData2, Tail);
        user_mod when is_atom(Val) ->
            ConnData2 = ConnData#conn_data{user_mod = Val},
            override_req_send_options(ReplyAction, ConnData2, Tail);
        user_args when is_list(Val) ->
            ConnData2 = ConnData#conn_data{user_args = Val},
            override_req_send_options(ReplyAction, ConnData2, Tail);
	trans_req when Val =:= false -> 
	    %% We only allow turning the transaction-sender off, since
	    %% the opposite (turning it on) would causing to much headake...
	    %% This will allow not using the transaction sender for
	    %% occasional messages
	    ConnData2 = ConnData#conn_data{trans_req    = Val, 
					   trans_sender = undefined},
	    override_req_send_options(ReplyAction, ConnData2, Tail);
        _Bad ->
            {error, {bad_send_option, {Key, Val}}}
    end;
override_req_send_options(_ReplyAction, ConnData, []) ->
    {ok, ConnData}.

override_rep_send_options(ConnData, [{Key, Val} | Tail]) ->
    case Key of
	protocol_version ->
            ConnData2 = ConnData#conn_data{protocol_version = Val},
            override_rep_send_options(ConnData2, Tail);	    
        send_handle ->
            ConnData2 = ConnData#conn_data{send_handle = Val},
            override_rep_send_options(ConnData2, Tail);
        reply_timer ->
            case megaco_config:verify_val(Key, Val) of
                true ->
                    ConnData2 = ConnData#conn_data{reply_timer = Val},
                    override_rep_send_options(ConnData2, Tail);
                false ->
                    {error, {bad_send_option, {Key, Val}}}
            end;
	trans_req when Val =:= false -> 
	    %% We only allow turning the transaction-sender off, since
	    %% the opposite (turning it on) would causing to much headake...
	    %% This will allow not using the transaction sender for
	    %% occasional messages
	    ConnData2 = ConnData#conn_data{trans_req    = Val, 
					   trans_sender = undefined},
	    override_rep_send_options(ConnData2, Tail);
        _Bad ->
            {error, {bad_send_option, {Key, Val}}}
    end;
override_rep_send_options(ConnData, []) ->
    {ok, ConnData}.


%% ----
%% This list is allways atleast one (list of actions) long.
%% ----
%% The proper number of transaction id numbers has already
%% been "allocated", and the connection data record is
%% updated accordingly.
encode_requests(#conn_data{trans_req    = true,
			   trans_sender = Pid,
			   serial       = LastSerial} = CD, ActionsList) 
  when is_pid(Pid) ->
    (catch encode_requests(CD, LastSerial, 
			   lists:reverse(ActionsList), [], []));
encode_requests(#conn_data{serial    = LastSerial} = CD, ActionsList) ->
    %% We shall not accumulate transactions. 
    %% This means that we shall not encode 
    %% the transactions individually (and send
    %% them to the sender process, which
    %% accumulate transactions for later sending),
    %% Instead we encode the entire message directly.
    %% => We shall return one binary, containing, 
    %%    possibly, many transactions
    encode_requests_in_msg(CD, LastSerial, lists:reverse(ActionsList)).


%% This means that we shall compose and encode one complete 
%% megaco message, containing one or more transactions.
encode_requests_in_msg(CD, LastSerial, ActionsList) ->
    TRs  = compose_requests_in_msg(LastSerial, ActionsList, []),
    Body = {transactions, TRs},
    Res  = megaco_messenger_misc:encode_body(CD, 
					     "encode trans request(s) msg", 
					     Body),
    case Res of
	{ok, Bin} ->
	    {ok, TRs, Bin};
	Error ->
	    Error
    end.

compose_requests_in_msg(_S, [], TRs) ->
    TRs;
compose_requests_in_msg(Serial, [A|As], Acc) ->
    TR = #'TransactionRequest'{transactionId = Serial,
                               actions       = A},
    compose_requests_in_msg(Serial - 1, As, [{transactionRequest, TR}|Acc]).


%% We have done the encoding in reverse order, so there
%% is no need to reverse now.
encode_requests(_, _, [], Serials, EncodedTRs) ->
    {ok, Serials, EncodedTRs}; 
encode_requests(CD, Serial, [Actions|ActionsList], Serials, EncodedTRs) ->
    case do_encode_request(CD, Serial, Actions) of
	{ok, Bin} ->
	    encode_requests(CD, Serial - 1, ActionsList, 
			    [Serial|Serials], [Bin|EncodedTRs]);
	Error ->
	    throw(Error)
    end.


do_encode_request(CD, Serial, Actions) ->
    TR = #'TransactionRequest'{transactionId = Serial,
			       actions       = Actions},
    megaco_messenger_misc:encode_trans_request(CD, TR).
    

imm_ack_req(Counter,  when_pending_sent) when (Counter > 0) -> 'NULL';
imm_ack_req(_Counter, when_pending_sent)                    -> asn1_NOVALUE;
imm_ack_req(_Counter, ImmAck)                               -> ImmAck.

maybe_send_reply(#conn_data{sent_pending_limit = Limit} = ConnData, 
		 TransId, Result, SendOpts, ImmAck) ->

    %% d("maybe_send_reply -> entry with"
    %%   "~n   Limit:    ~p"
    %%   "~n   TransId:  ~p"
    %%   "~n   Result:   ~p"
    %%   "~n   SendOpts: ~p"
    %%   "~n   ImmAck:   ~p", [Limit, TransId, Result, SendOpts, ImmAck]),

    %% Pending limit
    %% Before we can send the reply we must check that we have 
    %% not passed the pending limit (and sent an error message).
    case check_pending_limit(Limit, sent, TransId) of
	{ok, Counter} ->
	    case override_rep_send_options(ConnData, SendOpts) of
		{ok, ConnData2} ->
		    send_reply(ConnData2, Result, 
			       imm_ack_req(Counter, ImmAck));
		Error ->
		    Error
	    end;
	aborted ->
	    {error, aborted}
    end.

encode_reply(CD, TR) ->
    megaco_messenger_misc:encode_trans_reply(CD, TR).

send_reply(#conn_data{serial       = Serial,
		      trans_req    = TransReq,
		      trans_sender = TransSnd} = CD, TransRes, ImmAck) ->

    %% Encapsule the transaction result into a reply message

    %% d("send_reply -> entry with"
    %%   "~n   Serial:   ~p"
    %%   "~n   TransRes: ~p"
    %%   "~n   ImmAck:   ~p", [Serial, TransRes, ImmAck]),

    TR = #megaco_transaction_reply{transactionId        = Serial,
				   immAckRequired       = ImmAck,
				   transactionResult    = TransRes},
    case encode_reply(CD, TR) of
	{ok, Bin} when is_binary(Bin) andalso (TransReq =:= true) ->
	    ?rt2("send_reply - pass it on to the transaction sender", 
		 [size(Bin)]),
	    megaco_trans_sender:send_reply(TransSnd, Bin),
	    {ok, Bin};

        {ok, Bin} when is_binary(Bin) ->
	    ?rt2("send_reply - encoded", [size(Bin)]),
	    TraceLabel = "send trans reply", 
	    Body = {transactions, [Bin]},
            megaco_messenger_misc:send_body(CD, TraceLabel, Body);

        {ok, Bins} when is_list(Bins) ->
	    ?rt2("send_reply - encoded (segmented)", [length(Bins)]),
	    Res = send_reply_segments(CD, Bins),
	    {ok, Res};

	{error, not_implemented} ->
	    %% Oups, we cannot segment regardless the config,
	    %% so pack it all into one message and hope for
	    %% the best...
	    ?rt2("send_reply - cannot encode separate transactions", []),
	    TR2  = megaco_messenger_misc:transform_transaction_reply(CD, TR), 
	    Body = {transactions, [{transactionReply, TR2}]},
	    megaco_messenger_misc:send_body(CD, "encode trans reply", Body);

        {error, Reason} = Error ->
	    Code = ?megaco_internal_gateway_error,
	    Text = "encode transaction reply", 
	    ED   = #'ErrorDescriptor'{errorCode = Code, 
				      errorText = Text},
            Res = {transactionError, ED},
	    TR2 =  #megaco_transaction_reply{transactionId     = Serial, 
					     transactionResult = Res},
	    TR3 = megaco_messenger_misc:transform_transaction_reply(CD, TR2), 
            TraceLabel = "<ERROR> encode trans reply body failed",
            ?report_important(CD, TraceLabel, [TR, TR3, ED, Error]),
	    error_msg("failed encoding transaction reply body: ~s", 
		      [format_encode_error_reason(Reason)]),
            Body = {transactions, [{transactionReply, TR3}]},
            megaco_messenger_misc:send_body(CD, TraceLabel, Body),
	    Error
    end.

send_reply_segments(CD, Bins) ->
    TraceLabelPre = "send segmented trans reply", 
    (catch send_reply_segments(CD, TraceLabelPre, Bins)).

send_reply_segments(#conn_data{segment_send = infinity} = CD, Label, Bins) ->
    send_reply_segments(CD, Label, length(Bins), Bins);

send_reply_segments(#conn_data{segment_send = K} = CD, Label, Bins) 
  when is_integer(K) andalso (K =< length(Bins)) ->
    send_reply_segments(CD, Label, K, Bins);

send_reply_segments(#conn_data{segment_send = K} = CD, Label, Bins) 
  when is_integer(K) ->
    send_reply_segments(CD, Label, length(Bins), Bins).

send_reply_segments(CD, Label, K, Bins) ->
    send_reply_segments(CD, Label, K, Bins, []).

send_reply_segments(_CD, _Label, 0, Bins, Sent) ->
    ?rt2("send_reply_segments - done", [Sent, Bins]),
    {Sent, Bins};
send_reply_segments(CD, TraceLabelPre, K, [{SN, Bin}|Bins], Sent) ->
    case send_reply_segment(CD, TraceLabelPre, SN, Bin) of
	{ok, Bin2} ->
	    ?rt2("send_reply_segments - send", [K, SN]),
	    send_reply_segments(CD, TraceLabelPre, K-1, 
				Bins, [{SN, Bin2}|Sent]);
	Error ->
	    throw(Error)
    end.

send_reply_segment(CD, TraceLabelPre, SN, Bin) ->
    Label = lists:flatten(io_lib:format("~s[~w]", [TraceLabelPre, SN])),
    Body  = {transactions, [Bin]},
    megaco_messenger_misc:send_body(CD, Label, Body).
    

format_encode_error_reason(Reason) ->
    FS = 
	case Reason of
	    {Mod, Func, [EC, Msg], {AE, CS}} when is_atom(Mod)  andalso 
						  is_atom(Func) andalso
						  is_list(EC)   and
						  is_tuple(Msg) and
						  is_list(CS) ->
		io_lib:format("~n   Encode module: ~w"
			      "~n   Func:          ~w"
			      "~n   Encode config: ~w"
			      "~n   Message part:  ~p"
			      "~n   Actual error:  ~p" 
			      "~n   Call stack:    ~w", 
			      [Mod, Func, EC, Msg, AE, CS]);

	    {Mod, Func, [EC, Msg], AE} when is_atom(Mod)  andalso 
					    is_atom(Func) andalso
					    is_list(EC)   andalso
					    is_tuple(Msg) ->
		io_lib:format("~n   Encode module: ~w"
			      "~n   Func:          ~w"
			      "~n   Encode config: ~w"
			      "~n   Message part:  ~p"
			      "~n   Actual error:  ~p", 
			      [Mod, Func, EC, Msg, AE]);

	    {Mod, [EC, Msg], {AE, CS}} when is_atom(Mod)  andalso 
					    is_list(EC)   andalso
					    is_tuple(Msg) andalso
					    is_list(CS) ->
		io_lib:format("~n   Encode module: ~w"
			      "~n   Encode config: ~w"
			      "~n   Message part:  ~p"
			      "~n   Actual error:  ~p" 
			      "~n   Call stack:    ~w", 
			      [Mod, EC, Msg, AE, CS]);

	    {Mod, [EC, Msg], AE} when is_atom(Mod)  andalso 
				      is_list(EC)   andalso
				      is_tuple(Msg) ->
		io_lib:format("~n   Encode module: ~w"
			      "~n   Encode config: ~w"
			      "~n   Message part:  ~p"
			      "~n   Actual error:  ~p", 
			      [Mod, EC, Msg, AE]);

	    Error ->
		io_lib:format("~n   ~w", [Error])
	end,
    lists:flatten(FS).

			  
%% Presumably the user would return immediately (with {pending, Data}) if it 
%% knows or suspects a request to take a long time to process. 
%% For this reason we assume that handling a resent request 
%% could not have caused an update of the pending limit counter.
maybe_send_pending(#conn_data{sent_pending_limit = Limit} = ConnData, 
		   TransId) ->	    
    case check_and_maybe_incr_pending_limit(Limit, sent, TransId) of
	ok ->
	    send_pending(ConnData);
	error ->
	    SendReply = send_pending_limit_error(ConnData),
	    {aborted, SendReply};
	aborted ->
	    {aborted, ignore}
    end.


send_pending(#conn_data{serial       = Serial,
			trans_req    = true,
			trans_sender = Pid}) ->
    megaco_trans_sender:send_pending(Pid, Serial);
send_pending(#conn_data{serial = Serial} = CD) ->
    %% Encapsule the transaction result into a pending message
    TP = #'TransactionPending'{transactionId = Serial},
    Body = {transactions, [{transactionPending, TP}]},
    megaco_messenger_misc:send_body(CD, "send trans pending", Body).
	    

maybe_send_ack('NULL', #conn_data{serial       = Serial,
				  trans_ack    = true,
				  trans_sender = Pid}) ->
    megaco_trans_sender:send_ack_now(Pid, Serial);
maybe_send_ack('NULL', CD) ->
    send_ack(CD);
maybe_send_ack(_, #conn_data{auto_ack = false}) ->
    ignore;
maybe_send_ack(_, #conn_data{serial       = Serial,
			     trans_ack    = true,
			     trans_sender = Pid}) 
  when is_pid(Pid) ->
    %% Send (later) via the transaction sender
    megaco_trans_sender:send_ack(Pid, Serial),
    ok;
maybe_send_ack(_, CD) ->
    %% Send now
    send_ack(CD).


send_ack(#conn_data{serial = Serial} = CD) ->
    %% Encapsule the transaction result into a ack message
    TRA = #'TransactionAck'{firstAck = Serial},
    Body = {transactions, [{transactionResponseAck, [TRA]}]},
    megaco_messenger_misc:send_body(CD, "send trans ack", Body).


send_segment_reply(#conn_data{serial = Serial} = CD, SegNo) ->
    SR = #'SegmentReply'{transactionId = Serial,
			 segmentNumber = SegNo},
    Body = {transactions, [{segmentReply, SR}]},
    megaco_messenger_misc:send_body(CD, "send segment reply", Body).

send_segment_reply(#conn_data{serial = Serial} = CD, SegNo, Complete) ->
    SR = #'SegmentReply'{transactionId        = Serial,
			 segmentNumber        = SegNo,
			 segmentationComplete = Complete},
    Body = {transactions, [{segmentReply, SR}]},
    megaco_messenger_misc:send_body(CD, "send segment reply", Body).

send_segment_reply_complete(CD, SegNo) ->
    send_segment_reply(CD, SegNo, 'NULL').

    
send_pending_limit_error(ConnData) ->
    ?report_pending_limit_exceeded(ConnData),
    Code   = ?megaco_number_of_transactionpending_exceeded,
    Reason = "Pending limit exceeded",
    send_trans_error(ConnData, Code, Reason).
    
send_trans_error(ConnData, Code, Reason) ->
    %% Encapsulate the transaction error into a reply message
    ED       = #'ErrorDescriptor'{errorCode = Code, errorText = Reason},
    Serial   = ConnData#conn_data.serial,
    %% Version  = ConnData#conn_data.protocol_version,
    TransRes = {transactionError, ED}, 
    TR       = #megaco_transaction_reply{transactionId     = Serial, 
					 transactionResult = TransRes},
    TR2      = megaco_messenger_misc:transform_transaction_reply(ConnData, TR),
    Body     = {transactions, [{transactionReply, TR2}]},
    case megaco_messenger_misc:send_body(ConnData, "send trans error", Body) of
	{error, Reason2} ->
	    ?report_important(ConnData, 
			      "<ERROR> failed sending transaction error",
			      [Body, {error, Reason2}]),
	    error;
	_ ->
	    ok
    end.


send_message_error(ConnData, Code, Reason) ->
    ED = #'ErrorDescriptor'{errorCode = Code, errorText = Reason},
    Body = {messageError, ED},
    case megaco_messenger_misc:send_body(ConnData, "send trans error", Body) of
	{error, Reason2} ->
	    ?report_important(ConnData, 
			      "<ERROR> failed sending message error",
			      [Body, {error, Reason2}]),
	    error;
	_ ->
	    ok
    end.
	    

cancel(ConnHandle, Reason) when is_record(ConnHandle, megaco_conn_handle) ->
    case megaco_config:lookup_local_conn(ConnHandle) of
        [CD] ->
	    megaco_config:update_conn_info(CD, cancel, true),
	    do_cancel(ConnHandle, Reason, CD#conn_data{cancel = true}),
	    megaco_config:update_conn_info(CD, cancel, false),
	    ok;
        [] ->
	    ConnData = fake_conn_data(ConnHandle),
	    do_cancel(ConnHandle, Reason, ConnData)
    end.

do_cancel(ConnHandle, Reason, ConnData) ->
    ?report_trace(ConnData, "cancel", [ConnHandle, Reason]),
    LocalMid      = ConnHandle#megaco_conn_handle.local_mid,
    RemoteMid     = ConnHandle#megaco_conn_handle.remote_mid,
    ReqTransIdPat = #trans_id{mid = LocalMid, _ = '_'}, 
    ReqPat = #request{trans_id          = ReqTransIdPat,
		      remote_mid        = RemoteMid,
		      _                 = '_'},
    CancelReq = fun(Req) ->
			cancel_request(ConnData, Req, Reason),
			{_Type, Ref} = Req#request.timer_ref,  %% OTP-4843
			megaco_monitor:cancel_apply_after(Ref)
		end,
    Requests  = megaco_monitor:match_requests(ReqPat),
    lists:foreach(CancelReq, Requests),
    RemoteMid = ConnHandle#megaco_conn_handle.remote_mid,
    RepTransIdPat = #trans_id{mid = RemoteMid, _ = '_'}, % BUGBUG List here?
    RepPat = #reply{trans_id  = RepTransIdPat,
		    local_mid = LocalMid,
		    _         = '_'},
    CancelRep = fun(Rep) -> 
			cancel_reply(ConnData, Rep, Reason) 
		end,
    Replies   = megaco_monitor:match_replies(RepPat),
    lists:foreach(CancelRep, Replies),
    ok.

cancel_requests(_ConnData, [], _Reason)  ->
    ok;
cancel_requests(ConnData, [{transactionRequest,TR}|TRs], Reason) ->
    #'TransactionRequest'{transactionId = TransId0} = TR,
    TransId = to_local_trans_id(ConnData#conn_data.conn_handle, TransId0),
    case megaco_monitor:lookup_request(TransId) of
	[] ->
	    ignore;
	[Req] when is_record(Req, request) ->
	    cancel_request(ConnData, Req, Reason)
    end,
    cancel_requests(ConnData, TRs, Reason).

cancel_request(ConnData, Req, Reason)  ->
    ?report_trace(ignore, "cancel request", [Req]),
    ?TC_AWAIT_CANCEL_EVENT(),
    TransId   = Req#request.trans_id,
    Version   = Req#request.version,
    UserMod   = Req#request.user_mod,
    UserArgs  = Req#request.user_args,
    Action    = Req#request.reply_action,
    UserData  = Req#request.reply_data,
    UserReply = {error, Reason},
    ConnData2 = ConnData#conn_data{protocol_version = Version,
				   user_mod         = UserMod,
				   user_args        = UserArgs,
				   reply_action     = Action,
				   reply_data       = UserData},
    cancel_request2(ConnData2, TransId, UserReply).

cancel_request2(ConnData, TransId, UserReply) ->
    megaco_monitor:delete_request(TransId),
    megaco_monitor:request_lockcnt_del(TransId), 
    megaco_config:del_pending_counter(recv, TransId), % OTP-7189
    Serial    = TransId#trans_id.serial,
    ConnData2 = ConnData#conn_data{serial = Serial},
    return_reply(ConnData2, TransId, UserReply).
    

return_reply(ConnData, TransId, UserReply) ->
    Extra = ?default_user_callback_extra, 
    return_reply(ConnData, TransId, UserReply, Extra).

return_reply(ConnData, TransId, UserReply, Extra) ->
    ?report_trace(ConnData, "callback: trans reply", [UserReply]),
    Version  = ConnData#conn_data.protocol_version,
    UserData = ConnData#conn_data.reply_data,
    case ConnData#conn_data.reply_action of
        call when is_pid(UserData) ->
	    ?report_trace(ConnData, "callback: (call) trans reply", 
			  [UserReply]),
            Pid = UserData,
            Pid ! {?MODULE, TransId, Version, UserReply, Extra}; 
        cast ->
	    ?report_trace(ConnData, "callback: (cast) trans reply", [UserReply]),
	    UserMod    = ConnData#conn_data.user_mod,
	    UserArgs   = ConnData#conn_data.user_args,
            ConnHandle = ConnData#conn_data.conn_handle,
	    Args       = 
		case Extra of
		    ?default_user_callback_extra ->
			[ConnHandle, Version, UserReply, UserData | UserArgs];
		    _ ->
			[ConnHandle, Version, UserReply, UserData, Extra | UserArgs]
		end,
            Res = (catch apply(UserMod, handle_trans_reply, Args)),
	    ?report_debug(ConnData, "return: (cast) trans reply",
			  [UserReply, {return, Res}]),
	    case Res of
		ok ->
		    ok;
		_ ->
		    warning_msg("transaction reply callback failed: ~w", 
				[Res]),
		    ok
	    end,
	    Res;
        remote ->
	    ?report_trace(ConnData, "callback: (remote) trans reply", [UserReply]),
            Node = UserData,
            Args = [ConnData, UserReply, Extra],
            rpc:cast(Node, ?MODULE, receive_reply_remote, Args)
    end.

receive_reply_remote(ConnData, UserReply) ->
    Extra = ?default_user_callback_extra, 
    receive_reply_remote(ConnData, UserReply, Extra).

receive_reply_remote(ConnData, UserReply, Extra) ->
    TransId = to_local_trans_id(ConnData),
    case {megaco_monitor:request_lockcnt_inc(TransId),
	  (catch megaco_monitor:lookup_request(TransId))} of
        {Cnt, [Req]} when (Cnt =:= 1) andalso is_record(Req, request) -> 
            %% Don't care about Req and Rep version diff
	    do_receive_reply_remote(ConnData, TransId, Req, UserReply, Extra);

        {Cnt, [Req]} when is_integer(Cnt) andalso is_record(Req, request) -> 
            %% Another process is accessing, handle as unexpected 
	    %% (so it has a possibillity to get logged).
	    ?report_important(ConnData, "trans reply (no receiver)", 
			      [{user_reply,      UserReply}, 
			       {request_lockcnt, Cnt}]),
	    megaco_monitor:request_lockcnt_dec(TransId),
	    return_unexpected_trans_reply(ConnData, TransId, UserReply, Extra);

	%% no counter
        {_Cnt, [Req]} when is_record(Req, request) -> 
            %% The counter does not exist. 
	    %% This can only mean a code upgrade raise condition.
	    %% That is, this request record was created before 
	    %% this feature (the counters) was instroduced.
	    %% The simples solution to this is to behave exactly as
	    %% before, that is, pass it along, and leave it to the 
	    %% user to figure out.
	    ?report_trace(ConnData, 
			  "remote reply - "
			  "code upgrade raise condition", 
			  [{user_reply, UserReply}]),
	    do_receive_reply_remote(ConnData, TransId, Req, UserReply, Extra);

	{Cnt, _} when is_integer(Cnt) ->
	    ?report_trace(ConnData, "trans reply (no receiver)", 
			  [{user_reply, UserReply}, {request_lockcnt, Cnt}]),
	    megaco_monitor:request_lockcnt_dec(TransId), 
	    return_unexpected_trans_reply(ConnData, TransId, UserReply, Extra);

	_ ->
	    ?report_trace(ConnData, "remote reply (no receiver)", 
			  [{user_reply, UserReply}]),
	    return_unexpected_trans_reply(ConnData, TransId, UserReply, Extra)
    end.

do_receive_reply_remote(ConnData, TransId, 
			#request{timer_ref    = {_Type, Ref},
				 user_mod     = UserMod, 
				 user_args    = UserArgs,
				 reply_action = Action,
				 reply_data   = UserData} = _Req, 
			UserReply, Extra) ->
    megaco_monitor:delete_request(TransId),
    megaco_monitor:request_lockcnt_del(TransId), 
    megaco_monitor:cancel_apply_after(Ref),           % OTP-4843
    megaco_config:del_pending_counter(recv, TransId), % OTP-7189

    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
				   user_args    = UserArgs,
				   reply_action = Action,
				   reply_data   = UserData},
    return_reply(ConnData2, TransId, UserReply, Extra).


cancel_reply(ConnData, #reply{state     = waiting_for_ack,
			      user_mod  = UserMod,
			      user_args = UserArgs} = Rep, Reason) ->
    ?report_trace(ignore, "cancel reply [waiting_for_ack]", [Rep]),
    megaco_monitor:cancel_apply_after(Rep#reply.pending_timer_ref),
    Serial = (Rep#reply.trans_id)#trans_id.serial,
    ConnData2 = ConnData#conn_data{serial    = Serial,
				   user_mod  = UserMod,
				   user_args = UserArgs},
    T = #'TransactionAck'{firstAck = Serial},
    Extra = ?default_user_callback_extra, 
    handle_ack(ConnData2, {error, Reason}, Rep, T, Extra);

cancel_reply(_ConnData, #reply{state = aborted} = Rep, _Reason) ->
    ?report_trace(ignore, "cancel reply [aborted]", [Rep]),
    #reply{trans_id          = TransId,
	   timer_ref         = ReplyRef,
	   pending_timer_ref = PendingRef} = Rep,
    megaco_monitor:delete_reply(TransId),
    megaco_monitor:cancel_apply_after(ReplyRef),
    megaco_monitor:cancel_apply_after(PendingRef),     % Still running?
    megaco_config:del_pending_counter(sent, TransId),  % Still existing?
    ok;

cancel_reply(_ConnData, Rep, ignore) ->
    ?report_trace(ignore, "cancel reply [ignore]", [Rep]),
    #reply{trans_id          = TransId,
	   timer_ref         = ReplyRef,
	   pending_timer_ref = PendingRef} = Rep,
    megaco_monitor:delete_reply(TransId),
    megaco_monitor:cancel_apply_after(ReplyRef),
    megaco_monitor:cancel_apply_after(PendingRef),     % Still running?
    megaco_config:del_pending_counter(sent, TransId),  % Still existing?
    ok;

cancel_reply(_CD, _Rep, _Reason) ->
    ok.


request_keep_alive_timeout(ConnHandle, TransId) ->
    megaco_config:del_pending_counter(ConnHandle, TransId), 
    megaco_monitor:lookup_request(TransId),
    ok.

    
request_timeout(ConnHandle, TransId) ->
    ?rt1(ConnHandle, "request timeout", [TransId]),
    case megaco_monitor:lookup_request(TransId) of
	[] ->
	    request_not_found_ignore;
	[Req] when is_record(Req, request) ->
	    case megaco_config:lookup_local_conn(ConnHandle) of
		[CD] when (CD#conn_data.cancel =:= true) ->
		    cancel_in_progress_ignore;
		[CD] ->
 		    incNumTimerRecovery(ConnHandle),
 		    do_request_timeout(ConnHandle, TransId, CD, Req);
		[] when ConnHandle#megaco_conn_handle.remote_mid =:= preliminary_mid ->
 		    %% There are two possibillities:
		    %% 1) The connection has just been upgraded from a 
 		    %%    preliminary to a real connection. So this timeout
 		    %%    is just a glitch. E.g. between the removel of this
 		    %%    ConnHandle and the timer.
		    %% 2) The first message sent, the service-change, got no 
		    %%    reply (UDP without three-way-handshake).
		    %%    And then the other side (MGC) sends a request,
		    %%    which causes an auto-upgrade
 		    request_timeout_upgraded(ConnHandle, Req);
		[] ->
 		    incNumTimerRecovery(ConnHandle),
 		    ConnData = fake_conn_data(ConnHandle),
 		    do_request_timeout(ConnHandle, TransId, ConnData, Req)
	    end
    end.

request_timeout_upgraded(ConnHandle, Req) ->
    CD = fake_conn_data(ConnHandle),
    cancel_request(CD, Req, timeout).
    
do_request_timeout(ConnHandle, TransId, ConnData, 
		   #request{curr_timer = CurrTimer} = Req) ->

    ?rt1(ConnHandle, "process request timeout", [TransId, CurrTimer]),

    SendHandle = Req#request.send_handle,
    Version    = Req#request.version,
    ConnData2  = ConnData#conn_data{send_handle      = SendHandle,
				    protocol_version = Version},
    case CurrTimer of
	timeout ->  %%%%%%%
	    cancel_request(ConnData2, Req, timeout),
	    timeout1;

	%% Restartable timer 
	%% (max_retries = infinity_restartable)
	{_, timeout} ->  
	    cancel_request(ConnData2, Req, timeout),
	    timeout2;

	Timer ->
	    {SendOrNoSend, Data} = Req#request.bytes,
	    case SendOrNoSend of
		send ->
		    case maybe_encode(ConnData2, Data) of
			{ok, Bin} ->
			    ?report_trace(ConnData2, "re-send trans request", 
					  [{bytes, Bin}]),
			    case maybe_send_message(ConnData2, true, Bin) of
				ok ->
				    sent1_ignore;
				{ok, _} ->
				    sent2_ignore;
				{error, Reason} ->
				    ?report_important(ConnData2, 
						      "<ERROR> "
						      "re-send trans "
						      "request failed",
						      [{bytes, Bin}, 
						       {error, Reason}])
			    end;

			{error, Reason} ->
			    %% Since it was possible to encode the original
			    %% message this should really never happen...
			    ?report_important(ConnData2, 
					      "<ERROR> "
					      "re-send trans request failed",
					      [{transaction,
						Req#request.bytes}, 
					       {error, Reason}])
		    end;
		no_send ->
		    not_sent_ok
	    end,
	    {WaitFor, Timer2} = megaco_timer:restart(Timer),
	    OptBin            = opt_garb_binary(Timer2, Data),
	    {Type, _}         = Req#request.timer_ref,
	    M = ?MODULE,
	    F = request_timeout,
	    A = [ConnHandle, TransId],
	    Ref2 = megaco_monitor:apply_after(M, F, A, WaitFor),
	    NewFields = 
		[{#request.bytes,      {SendOrNoSend, OptBin}},
		 {#request.timer_ref,  {Type, Ref2}},
		 {#request.curr_timer, Timer2}],
	    megaco_monitor:update_request_fields(TransId, NewFields), % Timing problem
	    {restarted, WaitFor, Timer2}

    end.

maybe_encode(#conn_data{trans_req = false} = CD, {_Serial, Bin}) 
  when is_binary(Bin) ->
    Body = {transactions, [{transactionRequest, Bin}]},
    megaco_messenger_misc:encode_body(CD, "encode trans request msg", Body);
maybe_encode(_CD, {_Serial, Bin} = D) when is_binary(Bin) ->
    {ok, D};
maybe_encode(#conn_data{trans_req    = true,
			trans_sender = Pid} = CD, 
	     #'TransactionRequest'{transactionId = Serial} = TR) 
  when is_pid(Pid) ->
    case megaco_messenger_misc:encode_trans_request(CD, TR) of
	{ok, Bin} ->
	    {ok, {Serial, Bin}};
	Error ->
	    Error
    end;
maybe_encode(CD, TR) 
  when is_record(TR, 'TransactionRequest') ->
    Body = {transactions, [{transactionRequest, TR}]},
    megaco_messenger_misc:encode_body(CD, "encode trans request msg", Body);
maybe_encode(_CD, Trash) ->
    {error, {invalid_bin, Trash}}.

maybe_send_message(CD, Resend, Bin) when is_binary(Bin) ->
    megaco_messenger_misc:send_message(CD, Resend, Bin);
maybe_send_message(#conn_data{trans_sender = Pid}, _Resend, {Serial, Bin}) 
  when is_pid(Pid) andalso is_integer(Serial) andalso is_binary(Bin) ->
    megaco_trans_sender:send_req(Pid, Serial, Bin).

    
reply_timeout(ConnHandle, TransId, timeout) ->
    handle_reply_timer_timeout(ConnHandle, TransId);

%% This means that infinity_restartable was used for max_retries.
%% There is currently no reason to use this for the reply_timeout,
%% since there is no external event to restart the timer!
reply_timeout(ConnHandle, TransId, {_, timeout}) ->
    handle_reply_timer_timeout(ConnHandle, TransId);

reply_timeout(ConnHandle, TransId, Timer) ->
    ?report_trace(ConnHandle, "reply timeout", [Timer, TransId]),

    case lookup_reply(undefined, TransId) of
	[] ->
	    reply_not_found_ignore; 

	{Converted, 
	 #reply{state      = waiting_for_ack, 
		ack_action = {handle_ack, _}} = Rep} ->
	    case megaco_config:lookup_local_conn(ConnHandle) of
		[CD] when (CD#conn_data.cancel =:= true) ->
 		    cancel_in_progress_ignore;
		[CD] when (Converted =:= true) ->
 		    incNumTimerRecovery(ConnHandle),
		    %% When we did the reply record lookup, we had no
		    %% conn_data record, and the reply record was 
		    %% converted. This means that the reply record
		    %% has no valid info about user_mod or user_args.
		    %% Therefor, the user_mod and user_args of the 
		    %% conn_data record is better then nothing.
		    #conn_data{user_mod  = UserMod, 
			       user_args = UserArgs} = CD,
		    Rep2 = Rep#reply{user_mod  = UserMod, 
				     user_args = UserArgs}, 
 		    do_reply_timeout(ConnHandle, TransId, CD, Timer, Rep2);
		[CD] when (Converted =:= false) ->
 		    incNumTimerRecovery(ConnHandle),
 		    do_reply_timeout(ConnHandle, TransId, CD, Timer, Rep);
		[] ->
 		    incNumTimerRecovery(ConnHandle),
 		    CD = fake_conn_data(ConnHandle),
 		    do_reply_timeout(ConnHandle, TransId, CD, Timer, Rep)
	    end;

	{Converted, 
	 #reply{state      = waiting_for_ack, 
		bytes      = Sent} = Rep} when is_list(Sent) ->
	    case megaco_config:lookup_local_conn(ConnHandle) of
		[ConnData] when (Converted =:= true) ->
 		    incNumTimerRecovery(ConnHandle),
		    %% When we did the reply record lookup, we had no
		    %% conn_data record, and the reply record was 
		    %% converted. This means that the reply record
		    %% has no valid info about user_mod or user_args.
		    %% Therefor, the user_mod and user_args of the 
		    %% conn_data record is better then nothing.
		    #conn_data{user_mod  = UserMod, 
			       user_args = UserArgs} = ConnData,
		    Rep2 = Rep#reply{user_mod  = UserMod, 
				     user_args = UserArgs}, 
 		    do_reply_timeout(ConnHandle, TransId, ConnData, 
				     Timer, Rep2);
		[ConnData] when (Converted =:= false) ->
 		    incNumTimerRecovery(ConnHandle),
 		    do_reply_timeout(ConnHandle, TransId, ConnData, 
				     Timer, Rep);
		[] ->
 		    incNumTimerRecovery(ConnHandle),
 		    ConnData = fake_conn_data(ConnHandle),
 		    do_reply_timeout(ConnHandle, TransId, ConnData, 
				     Timer, Rep)
	    end;

	{_Converted, 
	 #reply{state = waiting_for_ack} = Rep} ->
	    do_reply_timeout(ConnHandle, TransId, Timer, Rep);
		    
	{_Converted, 
	 #reply{state = aborted} = Rep} ->
	    do_reply_timeout(ConnHandle, TransId, Timer, Rep);
		    
	_ ->
	    ignore
		
    end.

do_reply_timeout(ConnHandle, TransId, ConnData, Timer, 
		 #reply{send_handle = SH,
			version     = V,
			bytes       = Bytes} = Rep) when is_binary(Bytes) ->

%%     d("do_reply_timeout -> entry with"
%%       "~n   ConnHandle: ~p"
%%       "~n   TransId:    ~p"
%%       "~n   Timer:      ~p"
%%       "~n   Rep:        ~p"
%%       "~n", [ConnHandle, TransId, Timer, Rep]),

    CD = ConnData#conn_data{send_handle      = SH,
			    protocol_version = V},
    
    ?rt1(CD, "re-send trans reply", [{bytes, Bytes}]),
    case megaco_messenger_misc:send_message(CD, true, Bytes) of
	{ok, _} ->
	    ignore;
	{error, Reason} ->
	    ?report_important(CD, "<ERROR> re-send trans reply failed",
			      [{bytes, Bytes}, {error, Reason}])
    end,
    do_reply_timeout(ConnHandle, TransId, Timer, Rep);

do_reply_timeout(ConnHandle, TransId, ConnData, Timer, 
		 #reply{send_handle = SH,
			version     = V,
			bytes       = Sent} = Rep) when is_list(Sent) ->

%%     d("do_reply_timeout -> entry with"
%%       "~n   ConnHandle: ~p"
%%       "~n   TransId:    ~p"
%%       "~n   Timer:      ~p"
%%       "~n   Rep:        ~p"
%%       "~n", [ConnHandle, TransId, Timer, Rep]),

    CD = ConnData#conn_data{send_handle      = SH,
			    protocol_version = V},
    
    ReSend = 
	fun({SN, Bytes}) ->
		?rt1(CD, "re-send segmented trans reply", 
		     [{segment_no, SN}, {bytes, Bytes}]),
		case megaco_messenger_misc:send_message(CD, true, Bytes) of
%% 		    ok ->
%% 			ignore;
		    {ok, _} ->
			ignore;
		    {error, Reason} ->
			?report_important(CD, 
					  "<ERROR> re-send segmented "
					  "trans reply failed",
					  [{segment_no, SN}, 
					   {bytes,      Bytes}, 
					   {error,      Reason}])
		end
	end,
    lists:foreach(ReSend, Sent),
    do_reply_timeout(ConnHandle, TransId, Timer, Rep).

do_reply_timeout(ConnHandle, TransId, Timer, #reply{bytes = Bytes}) ->
    {WaitFor, Timer2} = megaco_timer:restart(Timer),
    OptBin = case Bytes of
		 Bin when is_binary(Bin) ->
		     opt_garb_binary(Timer2, Bin);
		 Sent when is_list(Sent) ->
		     Garb = fun(Bin) -> opt_garb_binary(Timer2, Bin) end,
		     [{SN, Garb(Bin)} || {SN, Bin} <- Sent]
	     end,
    M = ?MODULE,
    F = reply_timeout,
    A = [ConnHandle, TransId, Timer2],
    Ref2 = megaco_monitor:apply_after(M, F, A, WaitFor),
    NewFields = 
	[{#reply.bytes,     OptBin},
	 {#reply.timer_ref, Ref2}],
    megaco_monitor:update_reply_fields(TransId, NewFields), % Timing problem?
    {restarted, WaitFor, Timer2}.

    
handle_reply_timer_timeout(ConnHandle, TransId) ->
    ?report_trace(ConnHandle, "handle reply timeout", [timeout, TransId]),
    incNumTimerRecovery(ConnHandle),
    %% OTP-4378
    case lookup_reply(undefined, TransId) of
	{Converted, 
	 #reply{state = waiting_for_ack} = Rep} ->
	    Serial = (Rep#reply.trans_id)#trans_id.serial,
	    {Rep2, ConnData} = 
		case megaco_config:lookup_local_conn(ConnHandle) of
		    [ConnData0] when (Converted =:= false) ->
			#reply{user_mod  = UserMod,
			       user_args = UserArgs} = Rep, 
			{Rep, 
			 ConnData0#conn_data{user_mod  = UserMod,
					     user_args = UserArgs}};
		    [ConnData0] when (Converted =:= true) ->
			{Rep#reply{user_mod  = ConnData0#conn_data.user_mod,
				   user_args = ConnData0#conn_data.user_args},
			 ConnData0};
		    [] when (Converted =:= false) ->
			ConnData0 = fake_conn_data(ConnHandle),
			#reply{user_mod  = UserMod,
			       user_args = UserArgs} = Rep, 
			{Rep, 
			 ConnData0#conn_data{user_mod  = UserMod,
					     user_args = UserArgs}};
		    [] when (Converted =:= true) ->
			%% We have no valid info about user_mod and user_args 
			{Rep, fake_conn_data(ConnHandle)}
		end,
	    ConnData2 = ConnData#conn_data{serial = Serial},
	    T = #'TransactionAck'{firstAck = Serial},
	    Extra = ?default_user_callback_extra, 
	    handle_ack(ConnData2, {error, timeout}, Rep2, T, Extra);

	{_Converted, 
	 #reply{pending_timer_ref = Ref,              % aborted?
		bytes             = SegSent}} ->      % may be a binary
	    megaco_monitor:cancel_apply_after(Ref),
	    cancel_segment_timers(SegSent), 
	    megaco_monitor:delete_reply(TransId),
	    megaco_config:del_pending_counter(sent, TransId);
	[] ->
	    ignore_reply_removed
    end.
    
%% segment_reply_timeout(ConnHandle, TransId, SN, timeout) ->
%%     ?report_trace(ConnHandle, "segment reply timeout", [timeout, SN, TransId]),
%%     D = fun({_, _, SegRef}) ->
%% 		megaco_monitor:cancel_apply_after(SegRef)
%% 	end,
%%     incNumTimerRecovery(ConnHandle),
%%     %% OTP-4378
%%     case megaco_monitor:lookup_reply(TransId) of
%% 	[#reply{state = waiting_for_ack,
%% 		bytes = Sent} = Rep] ->
%% 	    Serial = (Rep#reply.trans_id)#trans_id.serial,
%% 	    ConnData = 
%% 		case megaco_config:lookup_local_conn(ConnHandle) of
%% 		    [ConnData0] ->
%% 			ConnData0;
%% 		    [] ->
%% 			fake_conn_data(ConnHandle)
%% 		end,
%% 	    ConnData2 = ConnData#conn_data{serial = Serial},
%% 	    T = #'TransactionAck'{firstAck = Serial},
%% 	    lists:foreach(D, Sent),
%%          Extra = ?default_user_callback_extra, 
%% 	    handle_ack(ConnData2, {error, timeout}, Rep, T, Extra);
%% 	[#reply{pending_timer_ref = Ref,
%% 		bytes             = Sent}] -> % aborted?
%% 	    lists:foreach(D, Sent),
%% 	    megaco_monitor:cancel_apply_after(Ref),
%% 	    megaco_monitor:delete_reply(TransId),
%% 	    megaco_config:del_pending_counter(sent, TransId);
	
%% 	[] ->
%% 	    ignore

%%     end.

%% segment_reply_timeout(ConnHandle, TransId, SN, Timer) ->
%%     ?report_trace(ConnHandle, "reply timeout", [Timer, SN, TransId]),

%% %%     d("reply_timeout -> entry with"
%% %%       "~n   ConnHandle: ~p"
%% %%       "~n   TransId:    ~p"
%% %%       "~n   Timer:      ~p", [ConnHandle, TransId, Timer]),

%%     case megaco_monitor:lookup_reply(TransId) of
%% 	[] ->
%% 	    ignore; % Trace ??

%% 	[#reply{state      = waiting_for_ack, 
%% 		bytes      = ack_action = {handle_ack, _}} = Rep] ->
%% 	    case megaco_config:lookup_local_conn(ConnHandle) of
%% 		[ConnData] ->
%%  		    incNumTimerRecovery(ConnHandle),
%%  		    do_reply_timeout(ConnHandle, TransId, ConnData, 
%% 				     Timer, Rep);
%% 		[] ->
%%  		    incNumTimerRecovery(ConnHandle),
%%  		    ConnData = fake_conn_data(ConnHandle),
%%  		    do_reply_timeout(ConnHandle, TransId, ConnData, 
%% 				     Timer, Rep)
%% 	    end;

%% 	[#reply{state = waiting_for_ack} = Rep] ->
%% 	    do_reply_timeout(ConnHandle, TransId, Timer, Rep);
		    
%% 	[#reply{state = aborted} = Rep] ->
%% 	    do_reply_timeout(ConnHandle, TransId, Timer, Rep);
		    
%% 	_ ->
%% 	    ignore
		
%%     end.


%% This clause is to catch the timers started prior to the code-upgrade
pending_timeout(#conn_data{conn_handle = CH}, TransId, Timer) ->
    ?report_trace(CH, "pending timeout(1)", [Timer, TransId]),
    pending_timeout(CH, TransId, Timer);

pending_timeout(ConnHandle, TransId, Timer) ->
    ?report_trace(ConnHandle, "pending timeout(2)", [Timer, TransId]),
    case megaco_config:lookup_local_conn(ConnHandle) of
	[CD] when (CD#conn_data.cancel == true) ->
	    cancel_in_progress_ignore;
	[CD] ->
	    Serial = TransId#trans_id.serial, 
	    handle_pending_timeout(CD#conn_data{serial = Serial}, 
				   TransId, Timer);
	[] ->
	    no_such_connection_ignore
    end.

handle_pending_timeout(CD, TransId, Timer) ->
    ?report_trace(CD, "handle pending timeout", []),
    case lookup_reply(CD, TransId) of
	{_Converted, 
	 #reply{state   = State,
		handler = Pid} = Rep} when (State =:= prepare) orelse  
					   (State =:= eval_request) ->

	    #conn_data{sent_pending_limit = Limit,
		       conn_handle        = ConnHandle} = CD,

	    %% ------------------------------------------
	    %% 
	    %%   Check pending limit
	    %% 
	    %% ------------------------------------------

	    case check_and_maybe_incr_pending_limit(Limit, sent, TransId) of
		ok ->

		    %% ---------------------------------------------
		    %% 
		    %%   1) Send pending message
		    %%   2) Possibly restart the pending timer
		    %% 
		    %% ---------------------------------------------

		    send_pending(CD),
		    case Timer of
			timeout ->
			    %% We are done
			    incNumTimerRecovery(ConnHandle),
			    timeout1;
			{_, timeout} ->
			    %% We are done
			    incNumTimerRecovery(ConnHandle),
			    timeout2;
			_ ->
			    {WaitFor, Timer2} = megaco_timer:restart(Timer),
			    M = ?MODULE,
			    F = pending_timeout,
			    A = [ConnHandle, TransId, Timer2],
			    PendingRef = 
				megaco_monitor:apply_after(M, F, A, WaitFor),
			    %% Timing problem?
			    megaco_monitor:update_reply_field(TransId, 
							      #reply.pending_timer_ref,
							      PendingRef),
			    {restarted, WaitFor, Timer2}
		    end;


		error ->

		    %% ------------------------------------------
		    %% 
		    %%   1) Send 506 error message to other side
		    %%   2) Notify user
		    %%   3) Set reply data in aborted state
		    %% 
		    %% -------------------------------------------

		    send_pending_limit_error(CD),
		    handle_request_abort_callback(CD, TransId, Pid),
		    %% Timing problem?
		    Rep2 = Rep#reply{state = aborted},
		    cancel_reply(CD, Rep2, aborted),
		    pending_limit_error;


		aborted ->

		    %% ------------------------------------------
		    %% 
		    %%   Pending limit already passed 
		    %% 
		    %% -------------------------------------------
		    Rep2 = Rep#reply{state = aborted},
		    cancel_reply(CD, Rep2, aborted),
		    pending_limit_aborted

	    end;
	[] ->
	    reply_not_found; % Trace ??

	{_Converted, 
	 #reply{state = waiting_for_ack}} ->
	    %% The reply has already been sent
	    %% No need for any pending trans reply
	    reply_has_been_sent;

	{_Converted, 
	 #reply{state = aborted} = Rep} ->
	    %% glitch, but cleanup just the same
	    cancel_reply(CD, Rep, aborted),
	    reply_aborted_state

    end.


segment_timeout(ConnHandle, TransId, timeout = Timer) ->
    ?report_trace(ConnHandle, "segment timeout", [TransId, Timer]),
    incNumTimerRecovery(ConnHandle),
    case megaco_monitor:lookup_request(TransId) of
	[] ->
	    timeout_not_found_ignore;
	
	[#request{seg_recv = Segs} = Req] ->
	    ConnData = 
		case megaco_config:lookup_local_conn(ConnHandle) of
		    [ConnData0] ->
			ConnData0;
		    [] ->
			fake_conn_data(ConnHandle)
		end,
	    Last = lists:last(lists:sort(Segs)),
	    All  = lists:seq(1,Last),
	    case All -- Segs of
		[] ->
		    %% The last segment has just arrived, ignore
		    ok;
		Missing ->
		    %% Send the error message
		    Code   = ?megaco_segments_not_received, 
		    Reason = missing_to_str(Missing),
		    send_message_error(ConnData, Code, Reason),

		    %% Report to the user
		    UserMod   = Req#request.user_mod,
		    UserArgs  = Req#request.user_args,
		    Action    = Req#request.reply_action,
		    UserData  = Req#request.reply_data,
		    UserReply = {error, {segment_timeout, Missing}},
		    ConnData2 = ConnData#conn_data{user_mod     = UserMod,
						   user_args    = UserArgs,
						   reply_action = Action,
						   reply_data   = UserData},
		    return_reply(ConnData2, TransId, UserReply)
	    end
    end;

segment_timeout(ConnHandle, TransId, Timer) ->
    ?report_trace(ConnHandle, "segment timeout", [TransId, Timer]),
    case megaco_monitor:lookup_request_field(TransId, #request.trans_id) of
	{ok, _} ->
	    {WaitFor, Timer2} = megaco_timer:restart(Timer),
	    M = ?MODULE,
	    F = segment_timeout,
	    A = [ConnHandle, TransId, Timer2],
	    Ref = megaco_monitor:apply_after(M, F, A, WaitFor),
	    %% Timing problem?
	    megaco_monitor:update_request_field(TransId, 
						#request.seg_timer_ref,
						Ref),
	    {restarted, WaitFor, Timer2};
	_ ->
	    not_found_ignore
    end.

%% segment_reply_timeout() ->
%%     ok.

missing_to_str(Missing) ->
    lists:flatten(missing_to_str2(Missing)).

missing_to_str2([X]) ->
    [integer_to_list(X)];
missing_to_str2([H|T]) ->
    [integer_to_list(H) , "," | missing_to_str2(T)].

return_unexpected_trans_reply(ConnData, TransId, 
			      {actionReplies, _} = UserReply, Extra) ->
    Trans = make_transaction_reply(ConnData, TransId, UserReply),
    return_unexpected_trans(ConnData, Trans, Extra);
return_unexpected_trans_reply(ConnData, TransId, 
			      {transactionError, _} = UserReply, Extra) ->
    Trans = make_transaction_reply(ConnData, TransId, UserReply),
    return_unexpected_trans(ConnData, Trans, Extra);
return_unexpected_trans_reply(CD, TransId, {error, Reason}, Extra) ->
    ?report_important(CD, "unexpected trans reply with error", 
		      [TransId, Reason, Extra]),
    ok;
return_unexpected_trans_reply(CD, TransId, Crap, Extra) ->
    ?report_important(CD, "unexpected trans reply with crap", 
		      [TransId, Crap, Extra]),
    ok.

return_unexpected_trans(ConnData, Trans) ->
    Extra = ?default_user_callback_extra, 
    return_unexpected_trans(ConnData, Trans, Extra).

return_unexpected_trans(ConnData, Trans0, Extra) ->
    UserMod    = ConnData#conn_data.user_mod,
    UserArgs   = ConnData#conn_data.user_args,
    ConnHandle = ConnData#conn_data.conn_handle,
    Version    = ConnData#conn_data.protocol_version,
    Trans      = transform_transaction_reply_enc(Version, Trans0),
    Args       = 
	case Extra of
	    ?default_user_callback_extra ->
		[ConnHandle, Version, Trans | UserArgs];
	    _ ->
		[ConnHandle, Version, Trans, Extra | UserArgs]
	end,
    Res = (catch apply(UserMod, handle_unexpected_trans, Args)),
    ?report_debug(ConnData, "return: unexpected trans", 
		  [Trans, {return, Res}]),
    case Res of
	ok ->
	    ok;
	_ ->
	    warning_msg("unexpected transaction callback failed: ~w", [Res]),
	    ok
    end,
    Res.
    

%%-----------------------------------------------------------------

to_remote_trans_id(#conn_data{conn_handle = CH, serial = Serial}) ->
    Mid = CH#megaco_conn_handle.remote_mid,
    #trans_id{mid = Mid, serial = Serial}.

to_local_trans_id(#conn_data{conn_handle = CH, serial = Serial}) ->
    Mid = CH#megaco_conn_handle.local_mid,
    #trans_id{mid = Mid, serial = Serial}.

to_local_trans_id(#conn_data{conn_handle = CH}, [S|_] = Serials) 
  when is_integer(S) ->
    Mid = CH#megaco_conn_handle.local_mid, 
    [#trans_id{mid = Mid, serial = Serial} || Serial <- Serials];
to_local_trans_id(#conn_data{conn_handle = CH}, 
		  [{transactionRequest, TR}|_] = TRs) 
  when is_record(TR, 'TransactionRequest') ->
    Mid = CH#megaco_conn_handle.local_mid, 
    [#trans_id{mid = Mid, serial = Serial} || 
	{transactionRequest, 
	 #'TransactionRequest'{transactionId = Serial}} <- TRs];

to_local_trans_id(#megaco_conn_handle{local_mid = Mid}, Serial) 
  when is_integer(Serial) ->
    #trans_id{mid = Mid, serial = Serial};
to_local_trans_id(#conn_data{conn_handle = CH}, Serial) 
  when is_integer(Serial) ->
    Mid = CH#megaco_conn_handle.local_mid, 
    #trans_id{mid = Mid, serial = Serial}.    
    

%%-----------------------------------------------------------------

transform_transaction_reply_dec({'TransactionReply', 
				 TransId, IAR, TransRes}) ->
    #megaco_transaction_reply{transactionId     = TransId, 
			      immAckRequired    = IAR, 
			      transactionResult = TransRes};
transform_transaction_reply_dec({'TransactionReply', 
				 TransId, IAR, TransRes,
				 SegNo, SegComplete}) ->
    #megaco_transaction_reply{transactionId        = TransId, 
			      immAckRequired       = IAR, 
			      transactionResult    = TransRes,
			      segmentNumber        = SegNo,
			      segmentationComplete = SegComplete}.

transform_transaction_reply_enc(
  3,
  #megaco_transaction_reply{transactionId        = TransId, 
			    immAckRequired       = IAR, 
			    transactionResult    = TransRes,
			    segmentNumber        = SegNo,
			    segmentationComplete = SegComplete}) ->
    {'TransactionReply', TransId, IAR, TransRes, SegNo, SegComplete};
transform_transaction_reply_enc(
  Version,
  #megaco_transaction_reply{transactionId        = TransId, 
			    immAckRequired       = IAR, 
			    transactionResult    = TransRes}) 
  when (Version < 3) ->
    {'TransactionReply', TransId, IAR, TransRes};
transform_transaction_reply_enc(_, TR) ->
    TR.
  
make_transaction_reply(#conn_data{protocol_version = Version}, 
		       TransId, TransRes) ->
    make_transaction_reply(Version, TransId, asn1_NOVALUE, TransRes).

%% make_transaction_reply(#conn_data{protocol_version = Version}, 
%% 		       TransId, IAR, TransRes) ->
%%     make_transaction_reply(Version, TransId, IAR, TransRes);

make_transaction_reply(3, TransId, IAR, TransRes) ->
    {'TransactionReply', TransId, IAR, TransRes, asn1_NOVALUE, asn1_NOVALUE};
make_transaction_reply(_, TransId, IAR, TransRes) ->
    {'TransactionReply', TransId, IAR, TransRes}.

  
%%-----------------------------------------------------------------

%% This function is used as a wrapper for reply-record lookups.
%% The intention is that during upgrade, this function
%% can perform on-the-fly conversions of reply-records.
lookup_reply(CD, TransId) ->
    case megaco_monitor:lookup_reply(TransId) of
	[#reply{} = Rep] ->
	    {false, Rep};

	%% Old (pre-3.13.1) version of the record => Convert to new version
	[{reply, TransId, 
	  LocalMid, State, PendingTmrRef, Handler, TimerRef, 
	  Version, Bytes, AckAction, SendHandle, Segments}] 
	when is_record(CD, conn_data) ->
	    #conn_data{user_mod  = UserMod, 
		       user_args = UserArgs} = CD,
	    Rep = #reply{trans_id          = TransId,
			 local_mid         = LocalMid,
			 state             = State, 
			 pending_timer_ref = PendingTmrRef,
			 handler           = Handler, 
			 timer_ref         = TimerRef,
			 version           = Version,
			 bytes             = Bytes, 
			 ack_action        = AckAction, 
			 send_handle       = SendHandle,
			 segments          = Segments, 
			 user_mod          = UserMod,
			 user_args         = UserArgs},
	    {true, Rep};

	%% Old (pre-3.13.1) version of the record => Convert to new version
	[{reply, TransId, 
	  LocalMid, State, PendingTmrRef, Handler, TimerRef, 
	  Version, Bytes, AckAction, SendHandle, Segments}] ->
	    %% ConnData is not known here, so ignore for now
	    Rep = #reply{trans_id          = TransId,
			 local_mid         = LocalMid,
			 state             = State, 
			 pending_timer_ref = PendingTmrRef,
			 handler           = Handler, 
			 timer_ref         = TimerRef,
			 version           = Version,
			 bytes             = Bytes, 
			 ack_action        = AckAction, 
			 send_handle       = SendHandle,
			 segments          = Segments},
	    {true, Rep};

	Else ->
	    Else
    end.


%%-----------------------------------------------------------------

%%-----------------------------------------------------------------
%% info_msg(F, A) ->
%%     ?megaco_info(F, A).

warning_msg(F, A) ->
    ?megaco_warning(F, A).

error_msg(F, A) ->
    ?megaco_error(F, A).


%%-----------------------------------------------------------------

%% d(F) ->
%%     d(F,[]).
%% 
%% d(F,A) ->
%%     d(true,F,A).
%%     %% d(get(dbg),F,A).
%% 
%% d(true,F,A) ->
%%     io:format("*** [~s] ~p:~p ***"
%% 	      "~n   " ++ F ++ "~n", 
%% 	      [format_timestamp(now()), self(),?MODULE|A]);
%% d(_, _, _) ->
%%     ok.
%% 
%% format_timestamp({_N1, _N2, N3} = Now) ->
%%     {Date, Time}   = calendar:now_to_datetime(Now),
%%     {YYYY,MM,DD}   = Date,
%%     {Hour,Min,Sec} = Time,
%%     FormatDate = 
%%         io_lib:format("~.4w:~.2.0w:~.2.0w ~.2.0w:~.2.0w:~.2.0w 4~w",
%%                       [YYYY,MM,DD,Hour,Min,Sec,round(N3/1000)]),  
%%     lists:flatten(FormatDate).

%% Time in milli seconds
t() ->
    {A,B,C} = os:timestamp(),
    A*1000000000+B*1000+(C div 1000).

	      
%%-----------------------------------------------------------------
%% Func: incNumErrors/0, incNumErrors/1, incNumTimerRecovery/1
%% Description: SNMP counter increment functions
%%-----------------------------------------------------------------
incNumErrors() ->
    incNum(medGwyGatewayNumErrors).

incNumErrors(CH) ->
    incNum({CH, medGwyGatewayNumErrors}).

incNumTimerRecovery(CH) ->
    incNum({CH, medGwyGatewayNumTimerRecovery}).

incNum(Cnt) ->
    case (catch ets:update_counter(megaco_stats, Cnt, 1)) of
	{'EXIT', {badarg, _Reason}} ->
	    ets:insert(megaco_stats, {Cnt, 1});
	Old ->
	    Old
    end.